unsafe and fixed block : Unsafe Code : Language Basics C# Source Code


Custom Search

C# Source Code » Language Basics » Unsafe Code »

 

unsafe and fixed block








    
 

public class MyValue
{
    public int id;
    public MyValue(int id) { this.id = id; }
}
   
class UnsafeClassApp
{
    unsafe public static void Swap(int* pi, int* pj)
    {
        int tmp = *pi;
        *pi = *pj;
        *pj = tmp;
    }
   
    static void Main(string[] args)
    {
        MyValue i = new MyValue(123);
        MyValue j = new MyValue(456);
        Console.WriteLine("Before Swap:\ti = {0}, j = {1}", i.id, j.id);
   
        unsafe
        {
             (int* pi = &i.id, pj = &j.id)
            {
                Swap(pi, pj);
            }
        }
   
        Console.WriteLine(
            "After Swap:\ti = {0}, j = {1}", i.id, j.id);
    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Language Basics
» Unsafe Code