I’m new to c# and read that instance fields were initialized before a default constructor call. Does that mean that they are doubly initialized?
class MyClass
{
public int value;
}
Would that mean that value gets the default 0 then the the default constructor is called and assigns 0 again?
No, the parameterless constructor created by the compiler doesn’t perform an assignment to the field unless you specify a variable initializer. So in a class like this:
… the generated constructor looks like this in IL:
Note the assignments to
aandbbut notc. Normally the difference between explicitly assigning a value of0and leaving it to be the default value is not observable, but it’s present in the IL. (A subclass which decided to call some virtual method before calling the base class constructor could demonstrate the difference, although I suspect that would violate the CLS.)