Unsafe swap : Pointer : Unsafe C# Examples


C# Examples » Unsafe » Pointer »

 

Unsafe swap









    
using  System;
using  System.Globalization;

public  class  MainClass{

    unsafe  public  static  void  UnsafeSwap(int*  i,  int*  j)
    {
        int  temp  =  *i;
        *i  =  *j;
        *j  =  temp;
    }


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



Output

Unsafe swap
Values before unsafe swap: i = 10, j = 20
Values after unsafe swap: i = 20, j = 10


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Unsafe
» Pointer