Swap two values : Ref : Language Basics C# Examples


C# Examples » Language Basics » Ref »

 

Swap two values









    
using  System;  
  
class  Swap  {  
    //  This  method  now  changes  its  arguments.  
    public  void  swap(ref  int  a,  ref  int  b)  {  
        int  t;  
    
        t  =  a;  
        a  =  b;  
        b  =  t;  
    }  
}  
  
class  MainClass  {  
    public  static  void  Main()  {  
        Swap  ob  =  new  Swap();  
  
        int  x  =  10,  y  =  20;  
  
        Console.WriteLine("x  and  y  before  call:  "  +  x  +  "  "  +  y);  
  
        ob.swap(ref  x,  ref  y);    
  
        Console.WriteLine("x  and  y  after  call:  "  +  x  +  "  "  +  y);  
    }  
}
    
   
  
   



Output

x and y before call: 10 20
x and y after call: 20 10


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Language Basics
» Ref