0

reference parameters is only useful for valuetypes? for example if you have a method that passes in a class, the ref keyword is useless?

public class someclass
{

}    

somefunction(ref someclass input)
{

}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

3 Answers3

1

You surely could use a ref parameter for a class type. For instance:

void MyClassFactory(ref MyClass newRef)
{
    newRef = new MyClass();
}

MyClass someRef = null;
MyClassFactory(ref someRef);
// Now someRef is referencing a new MyClass object.

And notice that the above code would not work without the ref keyword.

rsenna
  • 11,775
  • 1
  • 54
  • 60
  • 1
    Note in an example like that you're better off just returning the new object, not using a `ref` param. – Servy Jun 07 '13 at 14:33
  • 1
    @Servy: I never said this is *good* code ;) My intention here is just to illustrate that one may use `ref` for reference types, and that it has distinct semantics. – rsenna Jun 07 '13 at 14:34
  • If you're going to go out of your way to find an example, you could at least use one that's useful, otherwise you'll give the impression that it's not a useful feature. – Servy Jun 07 '13 at 14:35
  • @Servy: so why don't **you** do that. It's a free site after all. – rsenna Jun 07 '13 at 14:35
  • 2
    Because there's a million duplicates; I don't see the need to answer the question at all, and instead voted to close as a duplicate. – Servy Jun 07 '13 at 14:36
  • @Servy: Wow. I don't see how your last comment relates to your first. You first say that I should try to give a better answer. Than you say I should have not answered at all!! But anyway, I agree there are a lot of similar questions here on SO, so point taken... – rsenna Jun 07 '13 at 14:40
  • I asserted that this is not a good answer, and explained why. You asked me why I didn't provide a better answer, to which I stated that I didn't provide a better answer because there are already lots of good answers in duplicates. I didn't say that you shouldn't have answered at all, I simply stated that this is a low quality answer. If you decide to take the time to turn this into a high quality answer I'd suggest you post it on the canonical answer, rather than on a duplicate here. – Servy Jun 07 '13 at 14:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31410/discussion-between-rsenna-and-servy) – rsenna Jun 07 '13 at 14:43
0

ref can be useful for reference types if you need to replace the object the function was given with a new object. For example:

class MyClass
{

}

class MyClass2 : MyClass
{
   public MyClass2(MyClass original)
   {
   }
}

bool UpdateMyClass(ref MyClass input)
{
   bool success = false;

   if (input != null)
   {
       //Generate a new object with some additional functionality.
       input = new MyClassWithSuperPowers(input);
       success = true;
   }

   return success;
}

And of course, the most obvious use case is the string class.

void FormatString(ref string data)
{
  data = DateTime.Now + data;
}
Eric W
  • 579
  • 4
  • 14
0

My understanding is this(and I hope someone corrects me if I'm wrong):

C# contains value types(allocated on the stack), and reference types(allocated on the heap).

However, all parameters are passed by value by default, which means if you call a function

myFunc(myClass param) { }
...
myClass myVar = new myClass();
myFunc(myVar);
//myVar will not be changed here

Then a cheap copy of myVar will be created and passed into myFunc.

If you pass the same parameter by using the 'ref' keyword, then a copy of myVar is not made, and instead a reference to myVar is passed in. Then any changes made to myVar inside myFunc would be reflected in myVar once myFunc returns.

myFunc(ref myClass param) { }
...
myClass myVar = new myClass();
myFunc(ref myVar);
//myVar might be changed here

I'm having trouble finding an article that actually talks about parameters and not just value vs. refernece types, but I think this is how it works.

Shaz
  • 1,376
  • 8
  • 18