Constructor rules - please verify

  • Thread starter Thread starter Manco
  • Start date Start date
M

Manco

1. If you do not declare an explicit constructor, C-sharp will generate a
default paramaterless constructor that calls Object:ctor()

2. All derived class constructors implicity call base(), this can be
verified by looking at the MSIL.

3. If you declare a constructor in the base class with a parameter, but not
the paramaterless one, and you derive from this class, you will get a
compile error, because the C-sharp compiler wants to call base() and it
isn't there in the base class.
 
Manco said:
1. If you do not declare an explicit constructor, C-sharp will generate a
default paramaterless constructor that calls Object:ctor()
Pretty sure the default constructor calls base:ctor(), not Object:ctor(),
but I haven't checked the MSIL...
2. All derived class constructors implicity call base(), this can be
verified by looking at the MSIL.
yes, unless you provide a different call.
3. If you declare a constructor in the base class with a parameter, but not
the paramaterless one, and you derive from this class, you will get a
compile error, because the C-sharp compiler wants to call base() and it
isn't there in the base class.
You can avoid this by simply specifying the parameter to the constructor for
the base class:

Class MyClass : MyBaseClass
{
public MyClass() : base("parameter")
{
return;
}
}
 
1. If you do not declare an explicit constructor, C-sharp will generate a
default paramaterless constructor that calls Object:ctor()

Yep... To be more precise, it will call the parent class's parameterless
constructor, which could be explicitly written or implicitly created.
Eventually it will propagate to Object.ctor(). If the parent class has a
constructor with parameters and no parameterless constructor, a compiler
error will be generated.
2. All derived class constructors implicity call base(), this can be
verified by looking at the MSIL.
Yep.

3. If you declare a constructor in the base class with a parameter, but
not the paramaterless one, and you derive from this class, you will get a
compile error, because the C-sharp compiler wants to call base() and it
isn't there in the base class.

Yep. If you declare a constructor and remove the parameterless one, a
default constructor will not be created implicitly, hence the compiler
error.

I recommend taking a peek at the C# spec...very informative:
http://blogs.msdn.com/csharpfaq/archive/2004/03/11/87814.aspx

ShaneB
 
This looks correct but applies to classes only. Structs don't allow explicit
parameterless constructors whereas classes do. Of course you can't derive
from structs anyway. You won't get a compiler error if you invoke an
overloaded constructor in the base class explicitly, e.g., base(<params>). A
derived class constructor will only implicitly call the default base class
constructor if you don't have an explicit call.

The following compiles without error:

class A //has no default parameterless constructor
{
public A(int i) {}
}
class B: A
{
public B(): base(0) {}
}

Thomas P. Skinner [MVP]
 
Manco said:
1. If you do not declare an explicit constructor, C-sharp will generate a
default paramaterless constructor that calls Object:ctor()

Well, it calls the base class parameterless constructor.
2. All derived class constructors implicity call base(), this can be
verified by looking at the MSIL.

Unless you declare it to call a different constructor, either this(...)
or base(...).
3. If you declare a constructor in the base class with a parameter, but not
the paramaterless one, and you derive from this class, you will get a
compile error, because the C-sharp compiler wants to call base() and it
isn't there in the base class.

Only if you don't explicitly call that constructor from the derived
class too.

See http://www.pobox.com/~skeet/csharp/constructors.html
 
Back
Top