We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,605 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

General C# question - Inheritance

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 !

4
Contributors
4
Replies
2 Years
Discussion Span
1 Year Ago
Last Updated
5
Views
moroshko
Light Poster
30 posts since Dec 2009
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

see here :)

JugglerDrummer
Junior Poster
139 posts since Apr 2009
Reputation Points: 14
Solved Threads: 22
Skill Endorsements: 0

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]

MxDev
Junior Poster
142 posts since Sep 2007
Reputation Points: 8
Solved Threads: 3
Skill Endorsements: 0

Thank you both !

moroshko
Light Poster
30 posts since Dec 2009
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

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

Beata7
Newbie Poster
1 post since May 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0659 seconds using 2.69MB