Mega Code Archive

 
Categories / C# Tutorial / Unsafe
 

IntPtrs and unsafe bit-twiddling

Pointers are variables that hold the addresses of other variables. using System; using System.IO; using System.Reflection; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; public class MainClass {     public unsafe static void Main()     {         int a = 10;         Console.WriteLine("a is {0} ({0:X})", a);         IntPtr ip = new IntPtr(&a);         byte* pTarget = (byte*)ip.ToPointer() + 1;         *pTarget = 2;         Console.WriteLine("a is {0} ({0:X})", a);     } } a is 10 (A) a is 522 (20A)