Click here to Skip to main content
15,892,537 members
Home / Discussions / C#
   

C#

 
QuestionHow to return/store references on objects Pin
Anonymous26-Jan-04 7:42
Anonymous26-Jan-04 7:42 
AnswerRe: How to return/store references on objects Pin
Not Active26-Jan-04 8:52
mentorNot Active26-Jan-04 8:52 
GeneralRe: How to return/store references on objects Pin
Heath Stewart26-Jan-04 8:55
protectorHeath Stewart26-Jan-04 8:55 
GeneralRe: How to return/store references on objects Pin
Not Active26-Jan-04 9:56
mentorNot Active26-Jan-04 9:56 
GeneralRe: How to return/store references on objects Pin
Jörgen Sigvardsson26-Jan-04 10:14
Jörgen Sigvardsson26-Jan-04 10:14 
GeneralRe: How to return/store references on objects Pin
Not Active26-Jan-04 10:18
mentorNot Active26-Jan-04 10:18 
GeneralRe: How to return/store references on objects Pin
Jörgen Sigvardsson26-Jan-04 10:20
Jörgen Sigvardsson26-Jan-04 10:20 
GeneralRe: How to return/store references on objects Pin
Heath Stewart26-Jan-04 10:41
protectorHeath Stewart26-Jan-04 10:41 
For strings, yes, because they are immutable and handled a little differently. I agree that ref and out are handy even in managed code (they are often necessary when P/Invoking native methods, sometimes even with reference types (like for pointer pointers)), but in the case discussed here they are not.

Keep in mind, though, that objects do not require this - only value types and strings (guess I should'be mentioned that too, but at the time I didn't think about it). Take this little example:
using System;
 
public class Test 
{
  public static void Main()
  {
    string s = string.Empty;
    Console.WriteLine(s);
 
    Foo(s);
    Console.WriteLine(s);
 
    Test t = new Test();
    Console.WriteLine(t.s);
 
    Foo(t);
    Console.WriteLine(t.s);
  }
  private static void Foo(string s)
  {
    s = "Testing";
  }
  private static void Foo(Test t)
  {
    t.s = "Testing";
  }
 
  public string s;
 
  public Test()
  {
    s = "This is a test";
  }
}
The output would be:
 
 
This is a test
Testing


 

-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
GeneralRe: How to return/store references on objects Pin
Jörgen Sigvardsson26-Jan-04 12:41
Jörgen Sigvardsson26-Jan-04 12:41 
AnswerRe: How to return/store references on objects Pin
Heath Stewart26-Jan-04 8:53
protectorHeath Stewart26-Jan-04 8:53 
GeneralCEMAPI use in C# :confused: Pin
camasmartin26-Jan-04 7:08
camasmartin26-Jan-04 7:08 
GeneralRe: CEMAPI use in C# :confused: Pin
Heath Stewart26-Jan-04 8:47
protectorHeath Stewart26-Jan-04 8:47 
GeneralReferring to a 2D array through a 1D array Pin
crushinghellhammer26-Jan-04 6:50
crushinghellhammer26-Jan-04 6:50 
GeneralRe: Referring to a 2D array through a 1D array Pin
Anonymous26-Jan-04 8:21
Anonymous26-Jan-04 8:21 
GeneralRe: Referring to a 2D array through a 1D array Pin
crushinghellhammer26-Jan-04 14:25
crushinghellhammer26-Jan-04 14:25 
GeneralRe: Referring to a 2D array through a 1D array Pin
Heath Stewart26-Jan-04 9:06
protectorHeath Stewart26-Jan-04 9:06 
GeneralRe: Referring to a 2D array through a 1D array Pin
crushinghellhammer26-Jan-04 14:37
crushinghellhammer26-Jan-04 14:37 
GeneralRe: Referring to a 2D array through a 1D array Pin
Heath Stewart27-Jan-04 2:46
protectorHeath Stewart27-Jan-04 2:46 
GeneralRe: Referring to a 2D array through a 1D array Pin
crushinghellhammer27-Jan-04 8:21
crushinghellhammer27-Jan-04 8:21 
GeneralRe: Referring to a 2D array through a 1D array Pin
crushinghellhammer27-Jan-04 8:23
crushinghellhammer27-Jan-04 8:23 
GeneralRe: Referring to a 2D array through a 1D array Pin
Heath Stewart27-Jan-04 8:58
protectorHeath Stewart27-Jan-04 8:58 
GeneralRe: Referring to a 2D array through a 1D array Pin
crushinghellhammer27-Jan-04 9:01
crushinghellhammer27-Jan-04 9:01 
GeneralRe: Referring to a 2D array through a 1D array Pin
Heath Stewart27-Jan-04 9:08
protectorHeath Stewart27-Jan-04 9:08 
GeneralRe: Referring to a 2D array through a 1D array Pin
crushinghellhammer27-Jan-04 8:50
crushinghellhammer27-Jan-04 8:50 
GeneralPreview in Open Dialog box Pin
Shree26-Jan-04 6:23
Shree26-Jan-04 6:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.