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 PawarPosted Dec 27, 2011, 2:42 AM
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
Anuja PawarPosted Dec 27, 2011, 6:24 AM
VulpesPosted Dec 27, 2011, 6:23 AM
You can avoid the error by making the highlighted alteration:
Smart LuckyPosted Dec 27, 2011, 6:20 AM
i have write this program it is not running it is giving me error can any one tell me how can i do this................?
VulpesPosted Dec 27, 2011, 5:45 AM
VulpesPosted Dec 27, 2011, 4:32 AM
produces the following output: