Safe Swap : Pointer : Unsafe C# Examples


C# Examples » Unsafe » Pointer »

 

Safe Swap









    
using  System;
using  System.Globalization;

public  class  MainClass{

    public  static  void  SafeSwap(ref  int  i,  ref  int  j)
    {
        int  temp  =  i;
        i  =  j;
        j  =  temp;
    }

    static  void  Main(string[]  args)
    {
        int  i  =  10,  j  =  20;
        
        Console.WriteLine("Values  before  safe  swap:  i  =  {0},  j  =  {1}",  i,  j);
        SafeSwap(ref  i,  ref  j);
        Console.WriteLine("Values  after  safe  swap:  i  =  {0},  j  =  {1}",  i,  j);
    }
}
    
   
  
   



Output

Values before safe swap: i = 10, j = 20
Values after safe swap: i = 20, j = 10


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Unsafe
» Pointer