Are private members inherited in C#?

Petr picture Petr · Jun 1, 2010 · Viewed 73.6k times · Source

Just seen one tutorial saying that:

Class Dog
{
  private string Name;
}
Class SuperDog:Dog
{
 private string Mood;
}

Then there was an UML displaying that SuperDog will inherit Name as well. I have tried but to me it seems that only public members are inherited. At least I could not access Name unless it was declared as public.

Answer

Martin Eve picture Martin Eve · Jun 1, 2010

A derived class has access to the public, protected, internal, and protected internal members of a base class. Even though a derived class inherits the private members of a base class, it cannot access those members. However, all those private members are still present in the derived class and can do the same work they would do in the base class itself. For example, suppose that a protected base class method accesses a private field. That field has to be present in the derived class in order for the inherited base class method to work properly.

From: http://msdn.microsoft.com/en-us/library/ms173149.aspx

So, technically, yes, but practically, no.