Hi,

Say MyClass inherits from BaseClass, and I write the following constructor for MyClass:

public MyClass()
{
      DoSomething();
}

When MyClass constructor is called, does it call automatically the BaseClass constructor (even though I didn't explicitly wrote this) ?

Thanks !

Recommended Answers

All 4 Replies

Yes, if you do not specify which parent constructor to call, C# will automatically call the base class's parameterless constructor.

see here :)

Hi,
I think when you inherit a certain class, the parent or the class should first initialized with the constructor, even if you didn't call it explicitly.

To do this here's the code

public class _baseClass(){

public _baseClass(){
//some code here
}

public _baseClass(int j){
//some code here
}
} 

public class childClass : _baseClass{

public class childClass(int x) : base(x){
//some code here
}
}

I hope this benefits you, happy coding

[EL-Prince]

Thank you both !

Yes, at first is called the parent constructor, next child, here are complex explanation of inheritance:
SNIP

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.