You just need to use the unsafe
keyword
Also if you use resharper you can easily view the IL
Resharper -> windows -> IL Viewer
int number = 234;
unsafe
{
// Assign the address of number to a pointer:
int* p = &number;
// Print the value of *p:
System.Console.WriteLine("Value at the location pointed to by p: {0:X}", *p);
// Print the address stored in p:
System.Console.WriteLine("The address stored in p: {0}", (int)p);
}
Additional Resources
How to: obtain the address of a variable (C# Programming Guide)
To use unsafe
, you need to explicitly set the build setting
-unsafe (C# Compiler Options)
To set this compiler option in the Visual Studio development
environment
Open the project's Properties page.
Click the Build property page.
Select the Allow Unsafe Code check box.