Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
One of my colleagues speculated it would work like a double dereference in C++ and a little experimentation proved him correct.
The 'ref' keyword can be used to pass the reference of the reference to the object which allows the method to assign a different object to be returned via the parameter
[Test] public void TestPassingClassBasedObjectsByRef( ) { string str1 = "initial value";
PassStringInNormalWay( str1 ); Assert.AreEqual("initial value", str1, "str1 object is not altered" ); PassStringByReference( ref str1); Assert.AreEqual("a", str1, "str1 is now a different object"); }public void PassStringInNormalWay( string str ) { str = new string( 'new value', 1 ); }public void PassStringByReference( ref string str) { str = new string('new value', 1); }
Remember Me