Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do C# classes inherit constructors?

Tags:

c#

I just read http://blog.gurock.com/articles/creating-custom-exceptions-in-dotnet/

I don't know when it is written. It says:

"Since C# unfortunately doesn’t inherit constructors of base classes, this new type only has the standard constructor with no parameters and is therefore relatively useless."

This says the same in 2010: C#: inheriting constructors

Is this still true?

EDIT: Following on from answers, I'm sure there would be a way around the default parameterless constructor. Are there other reasons for lack of constructor inheritance?

like image 369
cja Avatar asked Feb 08 '13 09:02

cja


4 Answers

Constructors have never been inheritable in the entire lifetime of the C# language. That hasn't changed in C# 5.0: at the end of section 1.6.7.1 of the C# 5.0 spec, it still says:

Unlike other members, instance constructors are not inherited, and a class has no instance constructors other than those actually declared in the class. If no instance constructor is supplied for a class, then an empty one with no parameters is automatically provided.

So it still holds true today, and I imagine it will remain so in the foreseeable future.

like image 161
BoltClock Avatar answered Nov 15 '22 18:11

BoltClock


You have to explicitly call the constructor of the base class, unless the base class defines a default constructor. So yes they are not inherited.

Which sometimes lead to a bunch of boiler plate code where you do nothing than pass arguments from one constructor to another

public class NegativArgument : Exception {
     public NegativeArgument() : this("The number given was less than zero"){}
     public NegativeArgument(string message) : this(message,null){}
     public NegativeArgument(string message, Exception inner) : base:(message,inner){}
}

but what if you had an Exception type that should always have the same message? how would you solve that if the constructors were inherited? The exception class has a constructor that accepts a message so creating a new Exception type would in that case get that constructor too, not inheriting constructors makes it easy

public class NegativArgument : Exception {
     public NegativeArgument() : base("The number given was less than zero"){}
}

If the base class does not have a default constructor you will have a compile error if you do not explicitly call a base class constructor.

like image 42
Rune FS Avatar answered Nov 15 '22 19:11

Rune FS


Constructors are not inherited in C#.

If they were, then every class would have a default parameterless constructor (because all classes derive from Object and Object has a default parameterless constructor).

Many classes should only be constructed with specific values; this would be impossible to ensure if every class had a default parameterless constructor.

like image 33
Matthew Watson Avatar answered Nov 15 '22 18:11

Matthew Watson


You should call them explicitly the constructor of the base classes. They are not inheritable.

Didn't change anything about them.

Check out : Constructors (C# Programming Guide)

From the spec §1.6.7.1:

Unlike other members, instance constructors are not inherited, and a class has no instance constructors other than those actually declared in the class. If no instance constructor is supplied for a class, then an empty one with no parameters is automatically provided.

http://msdn.microsoft.com/en-us/library/ms228593.aspx

like image 24
Soner Gönül Avatar answered Nov 15 '22 18:11

Soner Gönül