Use 'out' keyword (no need to assign because it is an out) : Out : Language Basics C# Examples


C# Examples » Language Basics » Out »

 

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);

    }
}
    
   
  
   



Output

Adding 2 ints using out keyword
90 + 90 = 180


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Language Basics
» Out