Hi ....
I want to call constructor for a parent class ? How to call, please explain anyone with an example ?
Thanks.....
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jignesh TrivediPosted Feb 22, 2012, 6:14 AM
agree with "Vulpes".
A constructor can use the base keyword to call the constructor of a base class.
Please refer
http://msdn.microsoft.com/en-us/library/ms173115%28v=vs.80%29.aspx
http://stackoverflow.com/questions/12051/calling-base-constructor-in-c-sharp
hope this help.
Smart WavePosted Feb 22, 2012, 5:32 AM
class Derived : Base { public Derived(string someParams) : base("a string to be sent to parent's constructor" + someParams) { } }
Also, you'll find a good explanation hereVulpesPosted Feb 12, 2012, 7:26 AM
For example:
using System;
class Parent
{
public Parent()
{
Console.WriteLine("Hello from Parent's constructor which takes no parameters");
}
public Parent(string message)
{
Console.WriteLine(message);
}
}
class Child : Parent
{
public Child(string message) : base(message)
{
}
public Child() // calls base class's parameterless constructor automatically
{
}
}
class Test
{
static void Main()
{
Child c = new Child("Hello from Parent's constructor which takes a string parameter");
Child c2 = new Child();
Console.ReadKey();
}
}
Notice that the base class's parameterless constructor still gets called automatically even if you don't specify the ': base()' syntax.