I am very confused about the meaning of "default constructor" in C#. Many people, including my programming professor, just call any parameterless constructor "default constructor" (like in the questions and answers here or here). Even the SO tag wiki on default-constructor says that it is "a parameterless constructor... often (note that it says often, not always) generated by the compiler".
If this logic is to be followed, whether a constructor is "default" or not relies on how you can call it. I.e. if you can call it the "default" way, without any parameters, it is a default constructor.
However, MSDN, if I am reading it correctly, seems to give the name "default constructor" only to the parameterless constructor that is implicitly generated when no other constructor is specified, calls a parameterless base class constructor, and (like any other constructor) initializes the class
's fields to their values.
To think about it, this makes more sense than "default" == "parameterless"
: an implicitly generated constructor is sort of like the default option when you haven't chosen anything else.
So, is it correct to call all parameterless constructors default constructors?
Or is it correct to call explicitly defined constructors of the form public C(): base() {}
(for regular classes) and protected C(): base() {}
(for abstract classes) default constructors, as they fit the description of a default constructor on MSDN?
Or is the term really applicable only to implicitly generated constructors?
Also (probably the most important question): which is the most widely accepted definition of the term, be it correct or incorrect?
It's not just MSDN that uses the term "default constructor" specifically for a constructor which is supplied either "if you don't specify anything else" or "always, for a struct (pre C# 6)" or "always, for a struct, unless you specify your own parameterless constructor (C# 6)" - it's the C# language specification itself. Note that in the case of a struct, the compiler doesn't need to generate one for you in IL - it is present as part of the type system already.
Personally, I find it a useful distinction to make - particularly while specifically discussing constructor semantics. It's less useful to be pedantic about it when constructors are not the main point of discussion - for example, if someone is talking about LINQ, and describes the following code:
List<int> x = new List<int>();
x.Add(10);
int y = x.First();
... as calling the default constructor for List<T>
and then adding to it, etc - then there's no point in nitpicking that we're actually calling a parameterless constructor. (We can't even tell whether it's a default constructor without looking at the source - the concept only makes sense in the implementation, not from the outside, as it were.)
There's a further subtle distinction which even the specification gets wrong (and which I should raise a bug about). Consider this statement in the C# 5 specification:
If a class contains no instance constructor declarations, a default instance constructor is automatically provided. That default constructor simply invokes the parameterless constructor of the direct base class.
That's not necessarily the case. This code compiles fine, despite Base
not having a parameterless constructor:
public class Base
{
public Base(int x = 0)
{
Console.WriteLine("Base: x={0}", x);
}
}
public class Derived : Base
{
}
It would be more accurate to talk about the default constructor invoking a base class constructor as if an explicit call to base()
were present, with the normal process of overload resolution etc taking place.
Next:
Also (probably the most important question): which is the most widely accepted definition of the term, be it correct or incorrect?
I would say that most people don't even think about it, and use the two terms interchangably. It's not a matter of them thinking the definition is one way or another - I suspect the idea of defining it just doesn't occur to most people. (This is not to disparage them - it's just not something they find they need to care about. Not everyone wants to be a language lawyer.)
See more on this question at Stackoverflow