When no constructor is specified in a class, C # will automatically compile a public parameterless constructor, which will leave the fields all with the default values of each type. The default values for structs can vary from type to type, and classes are always null. The default values are always the values corresponding to the zeroed memory, that is, filled with bits 0.
To initialize the new instance
You can manually encode a parameterless constructor in order to change the default C # behavior, which is usually done to fill in the values of the fields with values that make sense, or else are more appropriate for an object being instantiated . This can also be done with field initializers, however, the code compiled by C # is as if the initialization were done inside the constructor itself:
private int field;
private string fieldStr;
public MyClass()
{
this.field = 10;
this.fieldStr = "xpto";
}
is exactly the same as:
private int field = 10;
private string fieldStr;
public MyClass()
{
this.fieldStr = "xpto";
}
To call another constructor, passing default parameters
Suppose a base class BaseClass
only has constructors with parameters. When inheriting from this class, you will be required to provide a constructor for the class, since C # will not generate one automatically. Therefore, you will have to code a constructor, either with or without parameters:
class MyClass : BaseClass
{
protected MyClass2()
: base("param") // a base só tem construtores com parâmetros
{
}
}
To change visibility
Another technique is to define a default constructor so that you can change its visibility, for example, to make it a protected constructor:
class MyClass
{
protected MyClass()
{
}
}
then you can only access the constructor like this:
class MyClass2 : MyClass
{
public MyClass2()
: base() // chamando o construtor de MyClass
{
}
}
Some encoding standards depend on a constructor with reduced visibility:
- Singleton: If the constructor were publicly visible, or even could be seen by heirs, multiple instances of the class could be created.
- Factory: If the constructor is visible, then other creative streams other than the object factory can be taken.
Instance, Structure, and Static Construction
Do not confuse instance constructor with static constructor. There are differences between these builders. The instance constructor has far fewer guarantees than the static constructor, and the purpose is different.
There are also differences between instance and structure builders. It is not possible to create a structure constructor without parameters, and the structure constructor must fill all the fields of the structure.
- static constructor: initializes static members, with the assurance that the constructor will be called only once (it is thread safe) before any use of the type. For generic types, the constructor is called for each type that is generated using the generic parameters. Also, if the static constructor throws an exception once, the exception is stored and relaunched every time the user type it again.
-
Instance constructor: Initializes the members of an instance of the class. It is not thread safe, as well as providing no particular warranty on reordering of readings and writes, which may cause a newly created object created on one thread is observed on another maliciously initialized thread .
It is necessary to instantiate before using any field of an object of class.
-
structure constructor: must initialize all members of the structure, and can not fall in the case of the question about constructors without parameters, because C # does not allow to define such a constructor. When using the default constructor of the structure, it is the same as using the defaut value of type:
new MinhaStruct() <==> default(MinhaStruct)
It is not necessary to call the struct constructor before setting the fields, if they are public:
TesteStruct t;
t.valor = 10;