Why below written code doesn't gives error even when string is a reference type.
public struct example
{
public int a;
public string name;
};
public void usestruct()
{
example objExample= new example();
MessageBox.Show(objExample.name);
}
EDIT
Modifying Jon Answer, I have few more Questions.
public struct Example
{
public int a;
public string name;
}
class Test
{
static void Main()
{
//Question 1
Example t1 = new Example();
t1.name = "First name";
Example t2 = t1;
t2.name = "Second name";
Console.WriteLine(t1.name); // Prints "First name"
Console.WriteLine(t2.name); // Prints "Second name"
Console.WriteLine(t1.name); // What will it Print ????
//Question 2
Example t1 = new Example();
t1.name = "First name";
Console.WriteLine(sizeof(t1)); // What will it Print ????
// I think it will print 4 + 4 bytes.
//4 for storing int, 4 for storing address of string reference.
//Considering
//int of 4 bytes
//Memory Address of 4 byte
//Character of 1 byte each.
//My collegue says it will take 4+10 bytes
//i.e. 4 for storing int, 10 bytes for storing string.
}
}
How many bytes will second case take.