static void Main(string[] args) { WriteLine("Initialize the class and struct"); var myClass = new MyClass { MyInt = 5 }; WriteLine($"myClass.MyInt: {myClass.MyInt}"); var myStruct = new MyStruct { MyInt = 7 }; WriteLine($"myStruct.MyInt: {myStruct.MyInt}"); WriteLine("Assign myClass a 2nd time"); var my2ndClass = myClass; WriteLine("Change the value in myClass"); myClass.MyInt = 10; WriteLine($"myClass.MyInt: {myClass.MyInt}"); WriteLine($"my2ndClass.MyInt: {my2ndClass.MyInt}"); WriteLine("Assign myStruct a 2nd time"); var my2ndStruct = myStruct; WriteLine("Change the value in myStruct"); myStruct.MyInt = 14; WriteLine($"myStruct.MyInt: {myStruct.MyInt}"); WriteLine($"my2ndStruct.MyInt: {my2ndStruct.MyInt}"); MyMutatingProcedure(myClass); WriteLine($"myClass.MyInt: {myClass.MyInt}"); WriteLine($"my2ndClass.MyInt: {my2ndClass.MyInt}"); MyMutatingProcedure(myStruct); WriteLine($"myStruct.MyInt: {myStruct.MyInt}"); WriteLine($"my2ndStruct.MyInt: {my2ndStruct.MyInt}"); }
private static void TimeStructs(int iterations) { WriteLine("Using structs ..."); var stopWatch = new Stopwatch(); stopWatch.Start(); for (int i = 0; i < iterations; i++) { MyStruct myStruct = new MyStruct { MyInt = randomNumberGenerator.Next(Max) }; } stopWatch.Stop(); WriteLine($"Time: {stopWatch.ElapsedMilliseconds} ms"); ; }
static void MyMutatingProcedure(MyStruct myStruct) { WriteLine("Inside MyMutatingProcedure(MyStruct myStruct)"); myStruct.MyInt = 21; WriteLine($"myStruct.MyInt: {myStruct.MyInt}"); }