something is not very clear to me while using c#, hope I can get some answers here
using System;
public struct SampleStruct {
public float v;
}
public class SampleClass {
public float v;
}
public class Program
{
private float aaa;
private float bbb;
private SampleClass c111 = new SampleClass();
private SampleClass c222 = new SampleClass();
private SampleStruct AAA = new SampleStruct();
private SampleStruct BBB = new SampleStruct();
public void Main()
{
//test int field
aaa = 10;
bbb = aaa;
bbb = 9;
Console.WriteLine("int test: " + aaa);
//test class
c111 = new SampleClass { v = 10 };
c222 = c111;
c222.v = 9;
Console.WriteLine("class test: " + c111.v);
//test struct
AAA = new SampleStruct { v = 10 };
BBB = AAA;
BBB.v = 9;
Console.WriteLine("class struct: " + AAA.v);
}
}
//output
//int test: 10
//class test: 9
//class struct: 10
My question is why a Class object behaviors differently than a struct here as you can see if I change the latter object it affects to the object where it copied from