C# (Sharp) Programming Language Question:
Download Job Interview Questions and Answers PDF
What is the syntax for calling an overloaded constructor within a constructor (this() and constructorname() does not compile)?
Answer:
The syntax for calling another constructor is as follows:
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() {}
}
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() {}
}
Download C# (Sharp) Programming Language Interview Questions And Answers
PDF
Previous Question | Next Question |
Why are there five tracing levels in System.Diagnostics.TraceSwitcher? | Why do I get a "CS5001: does not have an entry point defined" error when compiling? |