C # Builder Constructor Explicit vs implicit When creating an instance of a class or structure, generally using the implicit constructor, using the following syntax:
var MyClass = new MyClass ();
For classes, you can customize the constructor using the syntax:
public MyClass () {
/ / Here is the code of the class initialization
}
But in the case of structures , C # does not create a parameterless constructor, which would force us to include a parameter "dummy" just to bypass this restriction:
public MyStruct (object foo) {
/ / Here is the initialization code
}
MyStruct var = new MyStruct (null);
To avoid this, we can make use of the clause "static" to create a custom builder, which requires not prompted any parameter.
MyStruct public static New () {
MyStruct MyStruct obj = new ();
obj.Prop1 = 0;
obj.Prop2 = string . Emtpy;
return obj;}
then we can create our instance perfectly initialized using the syntax:
MyStruct MyStruct.New var = ();
Even better, this solution allows us to overload the constructor explicit with variations, according to the needs (which is not possible with the implicit constructor.)