Use out for reference type : ref « Language Basics « C# / CSharp Tutorial






using System;

class MyClass
{
   public int Val = 20;                     
}

class MainClass
{
   static void MyMethod(out MyClass f1, out int f2)
   {
      f1 = new MyClass();                   
      f1.Val = 25;                          
      f2 = 15;                              
   }

   static void Main()
   {
      MyClass myObject = null;
      int intValue;

      MyMethod(out myObject, out intValue);             

      Console.WriteLine("After  -- myObject.Val: {0}, intValue: {1}", myObject.Val, intValue);
   }
}
After  -- myObject.Val: 25, intValue: 15








1.11.ref
1.11.1.Use ref to pass a value type by reference
1.11.2.Swap two values
1.11.3.Swap two references.
1.11.4.Use out for reference type
1.11.5.Use ref for int value
1.11.6.Use ref for reference type
1.11.7.Change string using ref keyword
1.11.8.Passing Parameters by Value