If a base class has a bunch of overloaded constructors, and an inherited class has another bunch of overloaded constructors, can you enforce a call from an inherited constructor to an arbitrary base constructor?

Yes, just place a colon, and then keyword base (parameter list to invoke the appropriate constructor) in the overloaded constructor definition inside the inherited class.

Showing Answers 1 - 12 of 12 Answers

Ans:

yes,Yes, just place a colon, and then keyword base (parameter list to invoke the appropriate constructor) in the overloaded constructor definition inside the inherited class.

Simple Example

class B
{
B(int i)
{ }
}
class C : B
{
C() : base(5) // call base constructor B(5)
{ }
C(int i) : this() // call C()
{ }
public static void Main() {}

  Was this answer useful?  Yes

sarvesh

  • Apr 8th, 2006
 

The program you have posted results in compilation errors. The Lines with colon used either for extending base class or to call base class constructor, shows compile time errors. Please verify if there are specifications aboout that program.HAVE YOU TRIED IT URSELF.

  Was this answer useful?  Yes

Vishal

  • Aug 6th, 2006
 

Yes, just place a colon, and then keyword base (parameter list to invoke the appropriate constructor) in the overloaded constructor definition inside the inherited class.The sample example is belowed and i have checked it. It is working.public class Base1 { public Base1(int i) { } static void Main(string[] args) { } } public class Derived1 : Base1 { public Derived1() : base(5) { } }

  Was this answer useful?  Yes

Prasad

  • Nov 6th, 2007
 

Please try this...This is the corrected code. Please correct me if I am wrong

class B
{
 public B(int i)
 { }
}

class C : B
{
 C() : base(5) // call base constructor B(5)
 { }

 C(int i) : this() // call C()
 { }
 
 public static void Main() {}
}

Thanks!!
Prasad.

  Was this answer useful?  Yes

Just use keyword base(<<list of parameters>>) at the first line of code in respective derive class constructor.
Example:
Class A
{
 public A(string s)
 {
 }


 public A(int i, string s)
 {
 }
}

Class B:A
{
 public B(string s)
 {
  base(s);
  ...other code
 }

 public B(int i, string s)
 {
  base(i,s);
  ...other code
 }
}

  Was this answer useful?  Yes

poovarasan

  • Jan 20th, 2021
 

Use keyword base in the overloaded constructor definition inside the inherited class

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions