-3

If you have a method, one that takes in a variable, and doesn't actually modify the physical variable at all, should you use ref to reference the input parameter? It shouldn't really matter, because the variable isn't modified anyway, so what are the disadvantages/advantages of using ref? (in C# at least)

For example,

int[] numbers = new int[] { /* some numbers */ };
int getNumberValue(int index)
{
    return numbers[index];
}
int getNumberRef(ref int index)
{
    return numbers[index];
}

Why would you prefer any of the two methods over the other? They both work the same, since the parameter is never modified...

I would think that the ref version would be quicker if I used it 18 billion times, since the value version probably makes a clone of the parameter so the method can modify it (but I may be wrong), although there could be some disadvantages.

  • 2
    `I would think that the ref version would be quicker` http://ericlippert.com/2012/12/17/performance-rant/ – L.B Nov 28 '14 at 19:15
  • You want to slice bread. You can take a knife from the drawer or go buy a katana to slice the bread. Why would you use the katana if you clearly don't need it? Use the katana *when* you need one. (Bad analogy actually, katanas are freakin cool and I would use them to slice bread if I had one) – dcastro Nov 28 '14 at 19:40
  • @dcastro you are right, *Bad analogy*. – L.B Nov 28 '14 at 19:45
  • @L.B I'm not good with analogies :( but hopefully I got my point across – dcastro Nov 28 '14 at 19:49

3 Answers3

1

You dont use ref that way. ref really changes the reference of the variable passed. So you use this when you need it. Simple as that.

Unless you dont need a function or method to change the reference, you shouldnt use it at all, and usually its a good idea to avoid ref.

Ref works like this:

 int a = 5;
 int b = 6;


 _swap(ref a, ref b);


 // a is now 6 and b is now 5;

This is the Swap-Method:

 void _swap(ref int a, ref int b)
 { 
     int tmp = a;
     a = b;
     b = tmp;
 }

Test it here

CSharpie
  • 9,195
  • 4
  • 44
  • 71
  • I actually do know what `ref` does, I was just wondering if there were any valid reasons to use it when you don't need to. – user1319644 Nov 29 '14 at 15:46
0

Useful answer: you almost never need to use ref/out. It's basically a way of getting another return value, and should usually be avoided precisely because it means the method's probably trying to do too much. That's not always the case (TryParse etc are the canonical examples of reasonable use of out) but using ref/out should be a relative rarity.

More info here.

Community
  • 1
  • 1
0

They're functionally equivalent, but they're semantically very different.

A method with a signature such as Method(ref string s) tells clients it may need to assign s a new value. The client will consequently act upon this false assumption, and do things he doesn't need to, such as checking if s is null after calling your method, or some other kind of validation.

Also, as a general programming advice, don't use things you don't need! You're adding unnecessary complexity, you're gonna confuse yourself and others. This is the sort of behaviour that leads to cargo cult programming.

dcastro
  • 66,540
  • 21
  • 145
  • 155
  • I thought it would be quicker because `ref` doesn't have to clone the variable, but no.... Also [this](https://dotnetfiddle.net/HaL5e9) – user1319644 Nov 29 '14 at 16:02
  • @user1319644 The value is only *cloned* if it's a value type (e.g., `int`, `double`, `bool`). These values are so small that it doesn't really matter. Also, in some cases, the perf might even be worse - it's faster to clone a boolean (1 byte) than a pointer to a boolean (4/8 bytes) – dcastro Nov 29 '14 at 16:29
  • Also, ["premature optimization is the root of all evil"](http://stackoverflow.com/questions/385506/when-is-optimisation-premature). Don't concern yourself with such small details - focus on writing correct, flexible and maintainable software. If your application doesn't meet your performance requirements, then use a profiler to find the bottleneck - 99% of the time, the bottleneck is related to disk access, database or network connections. These are the things you should worry about. – dcastro Nov 29 '14 at 16:33