Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In C#, if I have an inherited class with a default constructor, do I have 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     } } 
like image 522
Guy Avatar asked Aug 20 '08 14:08

Guy


People also ask

What does |= mean in C?

The ' |= ' symbol is the bitwise OR assignment operator.

What is '~' in C programming?

In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...

What are operators in C?

C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.

What is the use of in C?

In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more.


2 Answers

You do not need to explicitly call the base constructor, it will be implicitly called.

Extend your example a little and create a Console Application and you can verify this behaviour 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.");         }     } } 
like image 140
Ian Nelson Avatar answered Sep 27 '22 23:09

Ian Nelson


It is implied, provided it is parameterless. 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's 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!     } } 
like image 45
Rob Cooper Avatar answered Sep 27 '22 23:09

Rob Cooper