C# nested class properties -


in c#, why nested class have instantiate it's parent class, reference parent class non-static properties in code?

public class outerclass {     public int outerclassproperty     {                 {             return 1;         }     }      public class innerclass     {         public int innerclassproperty         {                         {                 /* syntax error: cannot access non-static                  * member of outer type via nested type.                  */                 return outerclassproperty;             }         }     } } 

it seems have instead:

public class outerclass {     public int outerclassproperty     {                 {             return 1;         }     }      public class innerclass     {         public int innerclassproperty         {                         {                 outerclass implementedouterclass = new outerclass();                 return implementedouterclass.outerclassproperty;             }         }     } } 

i'm thinking first code example should okay, because if innerclass instantiated, parent class implemented first - along parent class properties.

thanks help, i'm trying learn in's , out's of c#... , not familiar java, comparing java won't much...

the behavior observing explicitly spelled out in c# specification. snippet of c# 5.0 below:

10.3.8.4 access
nested type , containing type not have special relationship regard this-access (§7.6.7). specifically, within nested type cannot used refer instance members of containing type. in cases nested type needs access instance members of containing type, access can provided providing instance of containing type constructor argument nested type.

the behavior of nested classes in c# different other language java inner classes in c# , c+ because c# different language created different language design team. exact historical reasons why particular behavior selected possibly found in blogs of members of c# design team, .net design guidelines book or msdn articles.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -