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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Feb 12, 2012, 2:26 AM EST
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.
Smart Wave4 reputationPosted Feb 22, 2012, 12:32 AM EST
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 hereJignesh TrivediRank #16 · 50.3K reputationPosted Feb 22, 2012, 1:14 AM EST
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.