CSharp - Inherited Members Hiding


A base class and a subclass may define identical members. For example:

class A{ 
  int Counter = 1; 
class B : A  { 
  int Counter = 2; 

Counter field in class B hides the Counter field in class A.

To hide a member intentionally, add the new modifier to the member in the subclass.

class A     { public     int Counter = 1; }
class B : A { public new int Counter = 2; }

new modifier tells your intent to the compiler and other programmers that the duplicate member is not an accident.

You can access hidden members by casting to the base class before invoking the function or access the field.