Calling the Base Constructor in C#

Calling the base constructor in C#

Modify your constructor to the following so that it calls the base class constructor properly:

public class MyExceptionClass : Exception
{
public MyExceptionClass(string message, string extrainfo) : base(message)
{
//other stuff here
}
}

Note that a constructor is not something that you can call anytime within a method. That's the reason you're getting errors in your call in the constructor body.

C#: Calling parent (Base) constructor from child's constructor body

This would do the same trick assuming u can access the properties directly

abstract class Parent 
{
protected int i;
protected Parent()
{
//default constructor
}
}

class Child : Parent
{
public Child(int i)
{

Int calculation = i * 2
base.i = calculation
}
}

however if u cant do that because of restricted access to the properties my personal preference is to outsource the logic of the calculation in separate function and call the base class like following:

abstract class Parent 
{
protected int i;
protected Parent(int i)
{
this.i = i;
}
}

class Child : Parent
{
public Child(int i) : base(Child.dosomework(i))
{
}

public static int dosomework(int i){
int calculation = i * 2
return calculation
}
}

Will the base class constructor be automatically called?

This is simply how C# is going to work. The constructors for each type in the type hierarchy will be called in the order of Most Base -> Most Derived.

So in your particular instance, it calls Person(), and then Customer() in the constructor orders. The reason why you need to sometimes use the base constructor is when the constructors below the current type need additional parameters. For example:

public class Base
{
public int SomeNumber { get; set; }

public Base(int someNumber)
{
SomeNumber = someNumber;
}
}

public class AlwaysThreeDerived : Base
{
public AlwaysThreeDerived()
: base(3)
{
}
}

In order to construct an AlwaysThreeDerived object, it has a parameterless constructor. However, the Base type does not. So in order to create a parametersless constructor, you need to provide an argument to the base constuctor, which you can do with the base implementation.

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

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.");
}
}
}

C# - Call base and class constructors in derived class

Not to say this is the CORRECT answer (and I'd love to hear how others would recommend I do this), but what I ended up doing was creating a private setter method that either constructor can call along the following lines:

class DerivedClass : BaseClass
{
public DerivedClass(string somethingNew)
: base()
{
setVals(somethingNew);
}
public DerivedClass(string somethingNew, int someVal)
: base(someVal)
{
setVals(somethingNew);
}

private setVals(string somethingNew)
{
// do something with the somethingNew varible
}
}

It saved my issue of having to deal with repetitive code and seems to be the cleanest way to do this, but, as I said, I'd love to see what others recommend / if there's a way to do this better.

Thanks!!!

How and when to call the base class constructor in C#

You can call the base class constructor like this:

// Subclass constructor
public Subclass()
: base()
{
// do Subclass constructor stuff here...
}

You would call the base class if there is something that all child classes need to have setup. objects that need to be initialized, etc...

Hope this helps.

Calling base constructor in C#

You have to call the base class constructor prior to the derived class constructor's body.

class Derived : Base
{
public Derived(string someParams)
: base("Blah " + someParams)
{

}

}

Call Constructor Base after Code Execution

There's a hacky way of doing it using an instance variable initializer:

using System;

class ClassA
{
public ClassA()
{
Console.WriteLine("Initialization");
}
}

class ClassB : ClassA
{
private readonly int ignoreMe = BeforeBaseConstructorCall();

public ClassB()
{
}

private static int BeforeBaseConstructorCall()
{
Console.WriteLine("Before new");
return 0; // We really don't care
}
}

class Test
{
static void Main()
{
new ClassB();
}
}

The less hacky way of doing it is to rethink how you construct a ClassB to start with. Instead of having clients call the constructor directly, provide a static method for them to call:

public static ClassB CreateInstance()
{
Console.WriteLine("Before initialization stuff");
return new ClassB();
}

Call base constructor and other constructor in constructor

No, you can't do it for the following reason:

When a constructor calls the constructor of its base, the latter call is THE PART of the constructor in question. So you can't call another constructor of the same class AND a constructor of a base class because the former call already contains a call to a base constructor - you can't initialize your base twice



Related Topics



Leave a reply



Submit