4

Possible Duplicate:
C#: What is the use of “ref” for Reference-type variables?

Hi,

Does it make sense to pass a "reference type" to a method as a parameter with 'ref' key?

Or it is just nonsense as it is already a reference type but not a value type?

Thanks!

Community
  • 1
  • 1
pencilCake
  • 51,323
  • 85
  • 226
  • 363

7 Answers7

5

It lets you change the reference variable itself, in addition to the object it's pointing to.

It makes sense if you think you might make the variable point to a different object (or to null) inside your method.

Otherwise, no.

Community
  • 1
  • 1
user541686
  • 205,094
  • 128
  • 528
  • 886
  • So, if I DO NOT pass as with 'ref' keyword and if I change its reference to null, then I return ffrom the method the object won't be null; correct? – pencilCake May 11 '11 at 08:02
  • @pencilCake: The reference won't be null, no. (An *object* is never "null".) – user541686 May 11 '11 at 08:09
  • So only in the scope of the method it will be behaved as it is null but once the method is left, it will still be as it was -not null-? – pencilCake May 11 '11 at 08:38
3

If makes a difference, because it allows the method to change the instance your variable is pointing to.

In other words, you use it when you want to make your variable point to a different instance of your reference type.

private static void WithoutRef(string s)
{
    s = "abc";
}

private static void WithRef(ref string s)
{
    s = "abc";
}

private static void Main()
{
    string s = "123";

    WithoutRef(s);
    Console.WriteLine(s); // s remains "123"

    WithRef(ref s);
    Console.WriteLine(s); // s is now "abc"
}
vgru
  • 49,838
  • 16
  • 120
  • 201
3

When passing a reference type as ref, you are passing the reference as a reference, and this might make sense. It means that the method can replace the reference, if it wishes to:

public void CallRef()
{
    string value = "Hello, world";
    DoSomethingWithRef(ref value);
    // Value is now "changed".
}

public void DoSomethingWithRef(ref string value) 
{
    value = "changed";
}
driis
  • 161,458
  • 45
  • 265
  • 341
0

ref in C# allows you to modify the actual variable.

Check out this question - What is the use of "ref" for reference-type variables in C#? - including this example

Foo foo = new Foo("1");

void Bar(ref Foo y)
{
    y = new Foo("2");
}

Bar(ref foo);
// foo.Name == "2"
Community
  • 1
  • 1
Stuart
  • 66,722
  • 7
  • 114
  • 165
0

It does if you want the incoming variable that is being passed in to have its pointer changed.

user541686
  • 205,094
  • 128
  • 528
  • 886
Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
  • 1
    @phoog: To regard references as pointers is a fairly good analogy. What do you think the relevant difference is here in this case? – Cody Gray - on strike May 11 '11 at 07:52
  • @Cody Gray: I'm paraphrasing Eric Lippert. For one thing, pointers identify a memory address, so you need to pin the pointer's target so the GC won't move it. Also, from the 2nd link below, a pointer variable is for "[manipulating] a variable itself as data, rather than manipulating the value of that variable as data." http://blogs.msdn.com/b/ericlippert/archive/2009/02/17/references-are-not-addresses.aspx; http://blogs.msdn.com/b/ericlippert/archive/2011/03/07/references-and-pointers-part-one.aspx; http://blogs.msdn.com/b/ericlippert/archive/2011/03/10/references-and-pointers-part-two.aspx – phoog May 11 '11 at 18:40
0

It's not nonsense. When you do that, you're passing the reference by reference.

Example:

class X
{
     string y;
     void AssignString(ref string s)
    {
        s = "something";
    }
    void Z()
    {
        AssignString(ref this.y};
    }
}
phoog
  • 42,068
  • 6
  • 79
  • 117
-1

Consider the following code. What do you expect shall be the output from this program?

string s = "hello world";
Console.WriteLine(s);

foo(s);
Console.WriteLine(s);

bar(ref s);
Console.WriteLine(s);

void foo(string x)
{
    x = "foo";
}

void bar(ref string x)
{
    x = "bar";
}

The output is:

hello world
hello world
bar

When calling the method bar, you're passing the reference to string s by reference (instead of by value), which means that s will then change at the call site.

Winston Smith
  • 21,585
  • 10
  • 60
  • 75