I know that String in .NET is a subclass of Object, and that Objects in .NET are reference type. Therefore, the following code puzzles me a little. Can you help me understand the difference? Thanks
I declared a class called MyInt:
class MyInt
{
int i;
public int number
{
get { return i; }
set { i = value; }
}
public override string ToString()
{
return Convert.ToString(i);
}
}
Then the following code:
MyInt a = new MyInt();
MyInt b = new MyInt();
a.number = 5;
b.number = 7;
b = a;
a.number = 9;
Console.WriteLine(a.ToString());
Console.WriteLine(b.ToString());
Yields:
9
9
Which I understand because this is an Object = Reference Type and so a and b now reference the same object on the heap.
But what happens here?
string a1;
string b1;
a1 = "Hello";
b1 = "Goodbye";
b1 = a1;
a1 = "Wazzup?";
Console.WriteLine(a1);
Console.WriteLine(b1);
This yields:
Wazzup?
Hello
Is String an object who is treated differently?