Mega Code Archive

 
Categories / C# Tutorial / Unsafe
 

Assign value of myInt using pointer

using System; using System.Globalization; public class MainClass{   public static void SomeUnsafeCode()   {     unsafe     {       int myInt;       int* ptrToMyInt = &myInt;              // Assign value of myInt using pointer.       *ptrToMyInt = 123;         // print out address.       Console.WriteLine("Value of myInt {0}", myInt);       Console.WriteLine("Address of myInt {0:X}", (int)ptrToMyInt);     }   }   static void Main(string[] args)   {         SomeUnsafeCode();   } } Value of myInt 123 Address of myInt 12F474