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 !
Related Article: C# question
is a C# discussion thread by Zarl that has 5 replies and was last updated 1 year ago.
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
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
© 2013 DaniWeb® LLC
Page rendered in 0.0659 seconds
using 2.69MB