Objects in .NET doesn't have fixed address, since they can be moved by GC. You need to tell GC to pin it with fixed statement if you would like to get that address. Please take a look at Understanding Memory References, Pinned Objects, and Pointers
You will need to use compile in "unsafe" mode (VS Project->Properties->Build and check "Allow unsafe code")
using System;
internal class Program
{
private static unsafe void Main(string[] args)
{
Point point = new Point { X = 10, Y = 20};
long a = 1;
long* pA = &a;
Console.WriteLine("'a' pointer value: \t{0}", *pA);
Console.WriteLine("'a' pointer address: \t{0:x16}", (long)pA);
Console.WriteLine("'point' value: \t\t{0:x16}", *(pA - 1));
// the location of 'point' on the stack
long prP = (long)(pA - 1);
long* pP = *(long**)prP;
Console.WriteLine("'point' address: \t{0:x16}", *pP);
Console.WriteLine("'point.Y' value: \t{0}", *(pP + 1));
Console.WriteLine("'point.X' value: \t{0}", *(pP + 2));
Console.ReadLine();
}
}
internal class Point
{
public int X;
public long Y;
}
Output:
'a' pointer value: 1
'a' pointer address: 000000001cb6dfa8
'point' value: 00000000027dd788
'point' address: 000007fe8a0851a8
'point.Y' value: 20
'point.X' value: 10