Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a better way to explain the behaviour differences between structs and classes in .net?
    primarykey
    data
    text
    <p>The code below shows a sample that I've used recently to explain the different behaviour of structs and classes to someone brand new to development. Is there a better way of doing so? (Yes - the code uses public fields - that's purely for brevity)</p> <pre><code>namespace StructsVsClasses { class Program { static void Main(string[] args) { sampleStruct struct1 = new sampleStruct(); struct1.IntegerValue = 3; Console.WriteLine("struct1.IntegerValue: {0}", struct1.IntegerValue); sampleStruct struct2 = struct1; Console.WriteLine(); Console.WriteLine("struct1.IntegerValue: {0}", struct1.IntegerValue); Console.WriteLine("struct2.IntegerValue: {0}", struct2.IntegerValue); struct1.IntegerValue = 5; Console.WriteLine(); Console.WriteLine("struct1.IntegerValue: {0}", struct1.IntegerValue); Console.WriteLine("struct2.IntegerValue: {0}", struct2.IntegerValue); sampleClass class1 = new sampleClass(); class1.IntegerValue = 3; Console.WriteLine(); Console.WriteLine("class1.IntegerValue: {0}", class1.IntegerValue); sampleClass class2 = class1; Console.WriteLine(); Console.WriteLine("class1.IntegerValue: {0}", class1.IntegerValue); Console.WriteLine("class2.IntegerValue: {0}", class2.IntegerValue); class1.IntegerValue = 5; Console.WriteLine(); Console.WriteLine("class1.IntegerValue: {0}", class1.IntegerValue); Console.WriteLine("class2.IntegerValue: {0}", class2.IntegerValue); Console.ReadKey(); } } struct sampleStruct { public int IntegerValue; } class sampleClass { public int IntegerValue; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload