In C # do you need to call the base constructor?

In C #, if I have an inherited class with a default constructor, do I need to explicitly call the base class constructor or will it be implicitly called?

class BaseClass { public BaseClass() { // ... some code } } class MyClass : BaseClass { public MyClass() // Do I need to put ": base()" here or is it implied? { // ... some code } } 
+44
inheritance constructor c #
Aug 20 '08 at 14:26
source share
7 answers

You do not need to explicitly reference the base constructor; it will be implicitly called.

Extend your example a bit and create a console application, and you can check this behavior for yourself:

 using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { MyClass foo = new MyClass(); Console.ReadLine(); } } class BaseClass { public BaseClass() { Console.WriteLine("BaseClass constructor called."); } } class MyClass : BaseClass { public MyClass() { Console.WriteLine("MyClass constructor called."); } } } 
+49
Aug 20 '08 at 14:32
source share

This is implied provided that it is without parameters. This is because you need to implement constructors that take values , see the code below for an example:

 public class SuperClassEmptyCtor { public SuperClassEmptyCtor() { // Default Ctor } } public class SubClassA : SuperClassEmptyCtor { // No Ctor this is fine since we have // a default (empty ctor in the base) } public class SuperClassCtor { public SuperClassCtor(string value) { // Default Ctor } } public class SubClassB : SuperClassCtor { // This fails because we need to satisfy // the ctor for the base class. } public class SubClassC : SuperClassCtor { public SubClassC(string value) : base(value) { // make it easy and pipe the params // straight to the base! } } 
+23
Aug 20 '08 at 15:03
source share

It is implied for basic parameterless constructors, but it is needed for default values ​​in the current class:

 public class BaseClass { protected string X; public BaseClass() { this.X = "Foo"; } } public class MyClass : BaseClass { public MyClass() // no ref to base needed { // initialise stuff this.X = "bar"; } public MyClass(int param1, string param2) :this() // This is needed to hit the parameterless ..ctor { // this.X will be "bar" } public MyClass(string param1, int param2) // :base() // can be implied { // this.X will be "foo" } } 
+7
Aug 20 '08 at 15:11
source share

This is implied.

+6
Aug 20 '08 at 14:28
source share

A derived class is built on a base class. If you think about it, the base object must be created in memory before the derived class can be added to it. Thus, the base object will be created on the way to creating the derived object. No, you do not call the constructor.

+4
Aug 20 '08 at 14:38
source share

AFAIK, you only need to call the base constructor if you need to pass any values ​​to it.

0
Aug 20 '08 at 14:28
source share

You do not need to call the base constructor explicitly, it will be implicitly called, but sometimes you need to pass parameters to the constructor, in this case you can do something like:

 using System; namespace StackOverflow.Examples { class Program { static void Main(string[] args) { NewClass foo = new NewClass("parameter1","parameter2"); Console.WriteLine(foo.GetUpperParameter()); Console.ReadKey(); } } interface IClass { string GetUpperParameter(); } class BaseClass : IClass { private string parameter; public BaseClass (string someParameter) { this.parameter = someParameter; } public string GetUpperParameter() { return this.parameter.ToUpper(); } } class NewClass : IClass { private BaseClass internalClass; private string newParameter; public NewClass (string someParameter, string newParameter) { this.internalClass = new BaseClass(someParameter); this.newParameter = newParameter; } public string GetUpperParameter() { return this.internalClass.GetUpperParameter() + this.newParameter.ToUpper(); } } } 

Note. If anyone knows a better solution, please let me know.

-3
Aug 20 '08 at 15:14
source share



All Articles