What are the keywords used to pass parameters to
the base class and how do I invoke other constructors.
Answer Posted / radhika
using base keyword we can pass parameters to the baseclass.
When an instance of the DerivedClass is created.The default
constructor in BaseClass is first invoked followed by the
DerivedClass . However, you
can choose which constructor you want to invoke in
BaseClass by using the base keyword in the
default constructor in DerivedClass , like this:
public class BaseClass
{
//---default constructor---
public BaseClass()
{
Console.WriteLine(“Default constructor in BaseClass”);
}
//---parameterized constructor---
public BaseClass(int x)
{
Console.WriteLine(“Parameterized Constructor in BaseClass”);
}
}
public class DerivedClass : BaseClass
{
//---default constructor---
public DerivedClass(): base(4)
{
Console.WriteLine(“Constructor in DerivedClass”);
}
}
| Is This Answer Correct ? | 5 Yes | 0 No |
Post New Answer View All Answers
Why do we need reflection in c#?
What is pure abstract class in c#?
What is the difference between dispose() and finalize() methods in c#?
what is the difference between the debug class and trace class?
Is java better than c#?
Can we change static variable value in c#?
What is view model in c#?
What is the difference between dictionary and hashtable in c#?
What is console readkey ()?
Does constructor return any value in c#?
How Do You Convert A Value-type To A Reference-type?
Why do canadians say zed?
Is string a data type in c#?
Is c# int immutable?
Can we call server-side code (c# or vb.net code) from javascript?