Skip to main content

Nothing go wrong for accessing a static member from an instance, in fact this is perfectly posiblepossible in other languages like java, youyour example code compilecompiles and runs ok if its in java.

ItsIt's a check the compiler designers introducehave introduced because they thinkthought that this helpswill help programmers to write clear code with this language. ThisOn this way only looking at the code you can always know if you are accessing a staticstatic or an instance member, in languages where this access is allow (like java) sometimes its a little confusing when someone decides to access a staticstatic member throughout an instance variable, in fact normally when you see this access in java code its more a mistake than a programmer really using this "language feature".

In my opinion limiting this access its a good decision in C# to avoid some mistakes and bad interpretations.

Although it's legal in Java to refer static members that way, it's recommended against in Code Conventions (10.2 Referring to Class Variables and Methods):

"Avoid using an object to access a class (static) variable or method. Use a class name instead..."

Nothing go wrong for accessing a static member from an instance, in fact this is perfectly posible in other languages like java, you example code compile and runs ok if its in java.

Its a check the compiler designers introduce because they think this helps programmers to write clear code with this language. This way only looking at the code you can always know if you are accessing a static or an instance member, in languages where this access is allow (like java) sometimes its a little confusing when someone decides to access a static member throughout an instance variable, in fact normally when you see this access in java code its more a mistake than a programmer really using this "language feature".

In my opinion limiting this access its a good decision in C# to avoid some mistakes and bad interpretations.

Nothing go wrong for accessing a static member from an instance, in fact this is perfectly possible in other languages like java, your example code compiles and runs ok if its in java.

It's a check the compiler designers have introduced because they thought that this will help programmers to write clear code with this language. On this way only looking at the code you can always know if you are accessing a static or an instance member, in languages where this access is allow (like java) sometimes its a little confusing when someone decides to access a static member throughout an instance variable, in fact normally when you see this access in java code its more a mistake than a programmer really using this "language feature".

In my opinion limiting this access its a good decision in C# to avoid some mistakes and bad interpretations.

Although it's legal in Java to refer static members that way, it's recommended against in Code Conventions (10.2 Referring to Class Variables and Methods):

"Avoid using an object to access a class (static) variable or method. Use a class name instead..."

Source Link
AlfredoCasado
  • 2.2k
  • 12
  • 11

Nothing go wrong for accessing a static member from an instance, in fact this is perfectly posible in other languages like java, you example code compile and runs ok if its in java.

Its a check the compiler designers introduce because they think this helps programmers to write clear code with this language. This way only looking at the code you can always know if you are accessing a static or an instance member, in languages where this access is allow (like java) sometimes its a little confusing when someone decides to access a static member throughout an instance variable, in fact normally when you see this access in java code its more a mistake than a programmer really using this "language feature".

In my opinion limiting this access its a good decision in C# to avoid some mistakes and bad interpretations.