i have make a program in which two nested classes one is outer class and other is inner class and just i have make their just constructor and i inherit them with the main class demo when ever i make an object of demo class by default outer class constructor is calling can any one tell me why it is calling although i am not making outer class object why it is calling....................? w8ing for rep
class demo :outerClass
{
static void Main(string[] arg)
{
demo d = new demo();
}
}
class outerClass
{
public outerClass()
{
Console.WriteLine ("This is outer constructor");
}
class innerClass
{
public innerClass()
{
Console.WriteLine("This is inner class constructor");
}
}
}

Anuja PawarRank #1,404 · 1.2K reputationPosted Dec 26, 2011, 9:42 PM EST
Your Demo Class does not have its own constructor, so it uses the outerclass constructor and you can see the results "This is outer constructor"
So Try thisclass demo : outerClass
{
public demo(): base("From derived")
{
Console.WriteLine("This is demo constructor");
Console.ReadKey();
}
static void Main(string[] arg)
{
demo d = new demo();
}
}
class outerClass
{
public outerClass()
{
Console.WriteLine("This is outer constructor");
Console.ReadKey();
}
public outerClass(string a)
{
Console.WriteLine(a);
}
class innerClass
{
public innerClass()
{
Console.WriteLine("This is inner class constructor");
Console.ReadKey();
}
}
}
For more details read this,
http://www.csharp-station.com/Tutorials/lesson08.aspx
VulpesRank #55 · 29.2K reputationPosted Dec 26, 2011, 11:32 PM EST
produces the following output:
VulpesRank #55 · 29.2K reputationPosted Dec 27, 2011, 12:45 AM EST
Smart LuckyRank #2,625 · 531 reputationPosted Dec 27, 2011, 1:20 AM EST
i have write this program it is not running it is giving me error can any one tell me how can i do this................?
VulpesRank #55 · 29.2K reputationPosted Dec 27, 2011, 1:23 AM EST
You can avoid the error by making the highlighted alteration:
Anuja PawarRank #1,404 · 1.2K reputationPosted Dec 27, 2011, 1:24 AM EST
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts