I know that its not a good practice to nest a class inside another class, but following is just for fun.
I have the following code
namespace PlayIt
{
class Class1
{
class Class2 : Class1
{
}
}
class SomeOtherClass
{
Class1 objClass1 = new Class1();
Class2 objClass2 = new Class2();
}
}
I am able to create the object of class1 but not of class2, Why So? Is there any way to access the class2 outside the class1
I am able to create the object of class1 but not of class2, Why So?
Two reasons:
Firstly, Class1
is implicitly internal, whereas Class2
is implicitly private (because it's nested).
Secondly, you're trying to use just Class2
in a scope where that has no meaning - you'd need to qualify it. This will work fine:
namespace PlayIt
{
class Class1
{
internal class Class2 : Class1
{
}
}
class SomeOtherClass
{
Class1 objClass1 = new Class1();
Class1.Class2 objClass2 = new Class1.Class2();
}
}
See more on this question at Stackoverflow