I know there are approximately 1,000 questions on the C# struct. I want to iterate I understand the value semantics, the performance benefits of stackallocs, etc. My specific question stems from this msdn article on when to use a struct over a class. MSDN struct vs class C#
First, they speak to the benefit of inline data for containers, specifically arrays. One allocation, and the un-user-definable default initialization of all structs in the array. However, they also emphasize that structs should be immutable.
If I declare a mystuct[] s = new mystruct[16];
, I have 16 mystruct
s with default values inline. If I have created a 'kosher' struct with no external mutability as recommended, how is the construct of any use to me? I doubt I intend to have an array of 0-integrals and nulls exclusively.
Do they mean immutable when functioning as a record or a property return only, ie singular data transport?
If the specific conflict between default array initialization, and recommended immutability has been broached before, please mark as a duplicate. Thanks for any insight.