Is it better to use structs over classes for serializing class settings?

Class settings can have initial default values.

    public class LevelSettings
        // Default level values
        public int Depth = 3;
        public int Height = 3;
        public TileSettings[] TileSettings = new TileSettings[27];
        public int[] TriangleIndices = new int[0];
        public Vector3[] TriangleVertices = new Vector3[0];
        public float TileSize = 1;
        public int Width = 3;

Structs will take up less memory (and load faster?) but I can't have default settings already set:

    public struct LevelSettings
        // Default level values
        public int Depth;
        public int Height;
        public TileSettings[] TileSettings;
        public int[] TriangleIndices;
        public Vector3[] TriangleVertices;
        public float TileSize;
        public int Width;

        public LevelSettings(int depth, int height, TileSettings[] tileSettings, int[] triangleIndices, Vector3[] triangleVertices, float tileSize, int width)
            Depth = depth;
            Height = height;
            TileSettings = tileSettings;
            TriangleIndices = triangleIndices;
            TriangleVertices = triangleVertices;
            TileSize = tileSize;
            Width = width;

So umm, thoughts anyone?

5 Years
Discussion Span
Last Post by nick.crane

Struct are value types and passed by value by default.
Classes are reference types and passed by reference.

This is the only fundamental difference between the two formats.
As far as I am aware there is no difference in the memory consumed by a class or struct with the same fields.
There is very little point in using structs for anything of any real size unless you are performing data manipulation on unmanaged memory.

The example you give is better done as a class as it contains arrays which are a reference type.
Consider the following types

public struct TestStruct
	// Default level values
	public int Value;
	public int[] ArrayData;

	public TestStruct(int value, int[] arrayData)
		Value = value;
		ArrayData = arrayData;

public class TestClass
	// Default level values
	public int Value = 3;
	public int[] ArrayData = new int[10];

When used they perform differently.

private void button2_Click(object sender, System.EventArgs e)
	// make two classes with same values 
	TestClass testClass = new TestClass();
	TestStruct testStruct = new TestStruct(3, new int[10]);

	// copy the class to a new variable
	var newTestClass = testClass;
	// change the value of a property on the new variable
	newTestClass.Value = testClass.Value + 20;
	// test if the values are the same
	if (testClass.Value == newTestClass.Value)
		MessageBox.Show("Class values MATCH");
		MessageBox.Show("Class values DO NOT match");

	// copy the struct to a new variable
	var newTestStruct = testStruct;
	// change the value of a property on the new variable
	newTestStruct.Value = testStruct.Value + 20;
	// test if the values are the same
	if (testStruct.Value == newTestStruct.Value)
		MessageBox.Show("Struct values MATCH");
		MessageBox.Show("Struct values DO NOT match");

	// now lets test the value of one of the array elements

	// change the value of a the first element on each of the four variables 
	testClass.ArrayData[0] = 10;
	newTestClass.ArrayData[0] = 20;
	testStruct.ArrayData[0] = 30;
	newTestStruct.ArrayData[0] = 40;

	// display the values of these
	var sb = new StringBuilder();
	sb.AppendLine("Array test values");
	sb.Append("Original class:");
	sb.Append("New class:");
	sb.Append("Original struct:");
	sb.Append("New struct:");
Votes + Comments
Very clear answer.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.