Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Unsafe Codes in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 34
    Comment on it

    C# allows use of pointer but it makes your programming code unsafe.

    A pointer is a variable which stores reference of another variable of same data type in which the pointer is declared.

     

    The general form of a pointer declaration is:

    type *var-name;

     

    Following are valid pointer declarations:

    int    *ip;    /* pointer to an integer */
    double *dp;    /* pointer to a double */
    float  *fp;    /* pointer to a float */
    char   *ch     /* pointer to a character */
    

     

    The following example illustrates use of pointers in C#, using the unsafe modifier:

     

    using System;
    namespace UnsafeCodeApplication
    {
       class Program
       {
          static unsafe void Main(string[] args)
          {
             int var = 20;
             int* p = &var;
             Console.WriteLine("Data is: {0} ",  var);
             Console.WriteLine("Address is: {0}",  (int)p);
             Console.ReadKey();
          }
       }
    }
    

     

    Retrieving the Data Value Using a Pointer

    By using ToString() method you can retrieve data from pointer variable. The following example demonstrates this:

     

    using System;
    namespace UnsafeCodeApplication
    {
       class Program
       {
          public static void Main()
          {
             unsafe
             {
                int var = 20;
                int* p = &var;
                Console.WriteLine("Data is: {0} " , var);
                Console.WriteLine("Data is: {0} " , p->ToString());
                Console.WriteLine("Address is: {0} " , (int)p);
             }
    
             Console.ReadKey();
          }
       }
    }
    

     

    Passing Pointers as Parameters to Methods

    You can pass a pointer variable to a method as parameter. The following example illustrates this:

     

    using System;
    namespace UnsafeCodeApplication
    {
       class TestPointer
       {
          public unsafe void swap(int* p, int *q)
          {
             int temp = *p;
             *p = *q;
             *q = temp;
          }
    
          public unsafe static void Main()
          {
             TestPointer p = new TestPointer();
             int var1 = 10;
             int var2 = 20;
             int* x = &var1;
             int* y = &var2;
    
             Console.WriteLine("Before Swap: var1:{0}, var2: {1}", var1, var2);
             p.swap(x, y);
    
             Console.WriteLine("After Swap: var1:{0}, var2: {1}", var1, var2);
             Console.ReadKey();
          }
       }
    }
    
    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: