C#: What style of data containers are preferred in general?
c#, data-containers, data-structures
Solution
Almost always a class; structs should really only be used for things that are values - for example, a complex-number, or a currency type/value pair - and should almost-without-exclusion be immutable.
A parameterless constructor is handy on mutable data if you are going to do data-binding, as this allows the system to create instances without additional code from yourself. A non-empty constructor is pretty-much essential for immutable data. For mutable data, an object initializer goes a long way towards that (although isn't quite the same in terms of validation etc):
var obj = new Person {Name="Fred", DateOfBirth=DateTime.Today};
Whether your types are immutable is up to you; mutable makes it easier to do data-binding and serialization. In general, you tend to see more mutable types in .NET, but this may change as we get into the parallel / many-core era.
Problem
When creating a simple data container class, what should it be? - Class or struct? - Mutable or immutable? - With or without non-empty constructor? Examples of the above: ``` struct MutableStruct { public string Text { get; set; } public int Number { get; set; } } struct ImmutableStruct { public string Text { get; private set; } public int Number { get; private set; } public ImmutableStruct(string text, int number) : this() { Text = text; Number = number; } } struct MutableStructWithConstructor { public string Text { get; set; } public int Number { get; set; } public MutableStructWithConstructor(string text, int number) : this() { Text = text; Number = number; } } class MutableClass { public string Text { get; set; } public int Number { get; set; } } class ImmutableClass { public string Text { get; private set; } public int Number { get; private set; } public ImmutableClass(string text, int number) { Text = text; Number = number; } } class MutableClassWithConstructor { public string Text { get; set; } public int Number { get; set; } public MutableClassWithConstructor(string text, int number) { Text = text; Number = number; } } ``` Any good reasons we should choose one above another? Or are there mostly subjective preferences that separate them? Or does it depend a lot on the spesific use cases? If so in what use cases should you choose what and why?