In C#, why does a nested class have to instantiate it's parent class, to reference its parent class non-static properties in code?
public class OuterClass
{
public int OuterClassProperty
{
get
{
return 1;
}
}
public class InnerClass
{
public int InnerClassProperty
{
get
{
/* syntax error: cannot access a non-static
* member of outer type via nested type.
*/
return OuterClassProperty;
}
}
}
}
It seems I have to do this instead:
public class OuterClass
{
public int OuterClassProperty
{
get
{
return 1;
}
}
public class InnerClass
{
public int InnerClassProperty
{
get
{
OuterClass ImplementedOuterClass = new OuterClass();
return ImplementedOuterClass.OuterClassProperty;
}
}
}
}
I'm thinking the first code example should be okay, because if InnerClass
is instantiated, the parent class would implemented first - along with the parent class properties.
Thanks for the help, I'm trying to learn the in's and out's of C#... and I am not familiar with Java, comparing to Java won't help much...