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.