- Home /
C#: Does a ref argument passed to another function still behave as a ref?
In the following example I want var to still be a ref inside someFunc(). Do i need to attach ref to the argument signature again, or will it still be a ref from the initial pass?
public void foo(ref MyStruct var){
someFunc(var);
}
public void someFunc(MyStruct var){
var;
}
My example has a struct being passed since according to http://www.yoda.arachsys.com/csharp/parameters.html that should mean it passes by value instead of reference as a class does. Am I right about that?
Should be pretty darn easy to test, try passing something into someFunc, changing it in there, then Debug.log after the call to someFunc inside of foo.
Answer by Dave-Carlile · Feb 11, 2013 at 09:56 PM
The ref
is required.
If you think about it a bit, what if someone called someFunc
directly and wanted to pass by ref? You would have to use the ref
syntax then right? It doesn't matter where a function is called from, if you want to pass a struct by reference, you have to use the ref
syntax.