Structures in C # can be created with or without a constructor call. In the case when the constructor is not called, the struct members will be initialized with default values ββ(essentially nullified), and the struct cannot be used until all its fields have been initialized.
From the documentation:
When creating a structure object using the new operator, it is created and the corresponding constructor is called. Unlike classes, structures can be instantiated without using a new operator. If you do not use the new one, the fields will remain unassigned, and the object cannot be used until all fields are initialized.
The following are some examples:
struct Bar { public int Val; public Bar( int v ) { Val = v; } } public void Foo() { Bar z;
Arrays of structures differ from one structural variable. When you declare an array of type struct, you declare a reference variable - as such, you must select it using the new operator:
Bar[] myBars = new Bar[10];
You can also use array initialization syntax if your structure has a constructor:
Bar[] moreBars = new Bar[] { new Bar(1), new Bar(2) };
You can become more complicated than that. If your struct has an implicit conversion operator from a primitive type, you can initialize it as follows:
struct Bar { public int Val; public Bar( int v ) { Val = v; } public static implicit operator Bar( int v ) { return new Bar( v ); } }
source share