Why Does the Default Parameterless Constructor Go Away When You Create One with Parameters

Why does the default parameterless constructor go away when you create one with parameters

There's no reason that the compiler couldn't add the constructor if you've added your own - the compiler could do pretty much whatever it wants! However, you have to look at what makes most sense:

  • If I haven't defined any constructor for a non-static class, I most likely want to be able to instantiate that class. In order to allow that, the compiler must add a parameterless constructor, which will have no effect but to allow instantiation. This means that I don't have to include an empty constructor in my code just to make it work.
  • If I've defined a constructor of my own, especially one with parameters, then I most likely have logic of my own that must be executed on creating the class. If the compiler were to create an empty, parameterless constructor in this case, it would allow someone to skip the logic that I had written, which might lead to my code breaking in all number of ways. If I want a default empty constructor in this case, I need to say so explicitly.

So, in each case, you can see that the behaviour of current compilers makes the most sense in terms of preserving the likely intent of the code.

Why does this does not create a default constructor?

Is not the parameterless constructor generated at compile-time ?

As others said, a default constructor will only be generated if you haven't provided a constructor implementation yourself which takes arguments.

From the specification (§10.10.4)(emphasis mine):

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. If the direct base class does not have an accessible
parameterless instance constructor, a compile-time error occurs.

If you look at the signature for your type in IL, you'll see that it creates a constructor with two parameters which are annotated with an [opt] tag and have default values:

.method public hidebysig specialname rtspecialname 
instance void .ctor (
[opt] int32 a,
[opt] int32 b
) cil managed
{
.param [1] = int32(0)
.param [2] = int32(0)
// Method begins at RVA 0x207c
// Code size 9 (0x9)
.maxstack 8

IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: nop
IL_0007: nop
IL_0008: ret
} // end of method foo::.ctor

This is not an empty constructor as Activate.CreateInstance expects.

why is there not always a default constructor

Having a custom constructor (usually) means that the internal state of the object is initialized with some custom information that you provide via constructor parameters. If you still had the default constructor in such a case, what would the initial state be?

If you have no custom constructor then it is assumed to be fine if you just use the default constructor because there is no internal state to initialize.



Related Topics



Leave a reply



Submit