c# - How do I make a struct immutable? -


all on stack overflow , internet see design principle keep structs immutable. unfortunately, never see implementation causes these structs immutable.

assuming struct not have reference types inside it, how make struct immutable? is, how prevent mutation of of primitive field (perhaps compile-time/runtime exception)?

i wrote simple test attempting make struct immutable, not using system.componentmodel.immutableobjectattribute worked:

class program {     static void main(string[] args)     {         immutablestruct immstruct1 = new immutablestruct();         console.writeline(immstruct1); //before mutation.          immstruct1.field1 = 1;         immstruct1.field2 = "hello";         immstruct1.field3 = new object();         console.writeline(immstruct1); //after 1st mutation.          immstruct1.field1 = 2;         immstruct1.field2 = "world";         immstruct1.field3 = new object();         console.writeline(immstruct1); //after 2nd mutation.          console.readkey();     } }  [immutableobject(true)] struct immutablestruct {     public int field1;     public string field2;     public object field3;      public override string tostring()     {         string field3string = "null";         if (field3 != null)         {             field3string = field3.gethashcode().tostring();         }         return string.format("field1: {0}, field2: {1}, field3: {2}", field1, field2, field3string);     } } 

make fields private readonly , pass initial values in constructor

public struct immutablestruct {     private readonly int _field1;     private readonly string _field2;     private readonly object _field3;      public immutablestruct(int f1, string f2, object f3)     {         _field1 = f1;         _field2 = f2;         _field3 = f3;     }      public int field1 { { return _field1; } }     public string field2 { { return _field2; } }     public object field3 { { return _field3; } } } 

starting c#6.0 (visual studio 2015) can use getter properties

public struct immutablestruct {     public immutablestruct(int f1, string f2, object f3)     {         field1 = f1;         field2 = f2;         field3 = f3;     }      public int field1 { get; }     public string field2 { get; }     public object field3 { get; } } 

note readonly fields , getter properties can initialized either in constructor or, in classes, field or property initializers public int field1 { get; } = 7;.

it not guaranteed constructor run on struct. e.g. if have array of structs, must call initializers explicitly each array element. arrays of reference types elements first initialized null, makes obvious have call new on each element. easy forget values types structs.

var immutables = new immutablestruct[10]; immutables[0] = new immutablestruct(5, "hello", new person()); 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -