Mega Code Archive

 
Categories / C# Tutorial / Language Basics
 

Use out keyword (no need to assign because it is an out)

An out parameter is used to pass a value out of a method. It is not necessary to give the variable used as an out parameter an initial value. An out parameter is always considered unassigned. The method must assign the parameter a value prior to the method's termination. using System; class MainClass {   public static void Add(int x,int y, out int ans)   {     ans = x + y;   }   public static void Main()    {          Console.WriteLine("Adding 2 ints using out keyword");     int ans;     Add(90, 90, out ans);     Console.WriteLine("90 + 90 = {0}\n", ans);   } } Adding 2 ints using out keyword 90 + 90 = 180