"Green" <ma*********@yahoo.com> wrote in message
news:Oo**************@TK2MSFTNGP09.phx.gbl...
Hi, All
According to C#.Net
string s = "foo";
string z = "foo";
s and z gonna share the same memory location, correct me if I am wrong. If
it is correct, how can I prove this. Is there any tool can show me the
memory location.
Thanks in advance!
s and z are different memory locations, they are the variables
(register/local allocated in this case) that contain the value of the
reference pointing to the GC heap allocated object of type string.
You can't take the contents of the variable, but you can take the address of
the first character of the string, so if you run following, you'll see that
both adresses are equal. When you change the contents of one of the strings,
you'll notice the addresses are different.
string a = "foo";
string b = "foo";
unsafe {
fixed (char* aa = a)
{
Console.WriteLine("{0:x}", (int)new IntPtr(aa));
}
fixed (char* aa = b)
{
Console.WriteLine("{0:x}", (int)new IntPtr(aa));
}
}
Willy.