Skip to main content

How to Resolve Warning "CS0108: 'member1' hides inherited member 'member2'" in C#

The Compiler Warning CS0108 relates to Inheritance and Name Shadowing. The message reads: "'Child.Method' hides inherited member 'Parent.Method'. Use the new keyword if hiding was intended."

In C#, when a derived class (Child) defines a member with the same name as a member in its base class (Parent), the compiler gets confused about your intentions. It essentially asks:

  1. Did you mean to Override the parent's logic (Polymorphism)?
  2. Did you mean to create a completely New member that just happens to share the name (Hiding/Shadowing)?
  3. Did you simply make a Naming Mistake?

This warning appears because C# defaults to "Hiding" but warns you that it might not be what you intended.

Understanding Member Hiding

Member hiding occurs when a child class introduces a member that is identical in name to one inherited from a parent class.

If you "Hide" a member, the link between Parent and Child is broken for that specific member.

  • If you access the object as the Child Type, you get the Child's version.
  • If you cast the object to the Parent Type, you get the Parent's version.

This is distinct from Overriding, where the Child's version is executed regardless of how the variable is cast.

Scenario 1: Accidental Naming Collision

Often, this warning happens simply because you didn't realize the base class already had a member with that name.

Example of error

public class BaseEntity
{
public int Id { get; set; }
}

public class User : BaseEntity
{
// ⛔️ Warning CS0108: 'User.Id' hides inherited member 'BaseEntity.Id'.
// The BaseEntity already has an 'Id'. Defining it again here hides the original.
public string Id { get; set; }
}

Solution: Rename the Member

If you do not strictly need the name to be identical, rename the member in the child class to be more specific.

public class User : BaseEntity
{
// ✅ Correct: 'UserId' is unique, and 'Id' is inherited from BaseEntity.
public string UserId { get; set; }
}

Scenario 2: Intentional Hiding (Using 'new')

Sometimes you want to hide the parent member. For example, the Parent has a method GetDetails() that returns a string, but in the Child, you want GetDetails() to return a complex object. Since the return types differ, you cannot use override. You must use Shadowing.

To silence the warning and tell the compiler "I did this on purpose," use the new modifier.

Example of code with warning

public class Parent
{
public void Show() => System.Console.WriteLine("Parent");
}

public class Child : Parent
{
// ⛔️ Warning CS0108: Explicitly missing 'new' or 'override'
public void Show() => System.Console.WriteLine("Child");
}

Solution: Add 'new'

public class Child : Parent
{
// ✅ Correct: The 'new' keyword acknowledges the hiding.
// The warning disappears.
public new void Show() => System.Console.WriteLine("Child");
}
note

Behavior Warning: When using new, the method called depends on the variable type: Parent p = new Child(); p.Show(); -> Prints "Parent". Child c = new Child(); c.Show(); -> Prints "Child".

Scenario 3: Missing 'override' (Polymorphism)

The most dangerous cause of CS0108 is when you actually intended to use Polymorphism (Overriding) but forgot the keyword. In this case, your code behaves differently than expected when passed to methods accepting the base class.

Example of error

public class Animal
{
public virtual void Speak() => System.Console.WriteLine("Generic Sound");
}

public class Dog : Animal
{
// ⛔️ Warning CS0108: Did you mean to override?
// Without 'override', this is treated as 'new', breaking polymorphism.
public void Speak() => System.Console.WriteLine("Bark");
}

Solution: Use 'override'

If the base member is marked virtual or abstract and you want to replace its behavior globally for this object, use override.

public class Dog : Animal
{
// ✅ Correct: This ensures "Bark" is printed even if the variable is type 'Animal'.
public override void Speak() => System.Console.WriteLine("Bark");
}

Conclusion

CS0108 is a warning about ambiguity in your inheritance chain.

  1. Check for Accidents: Did you accidentally name a variable the same as something in the base class? Rename it.
  2. Check for Polymorphism: Did you mean to extend the base class logic? Use override (and ensure the base is virtual).
  3. Check for Shadowing: Do you explicitly want to replace the member and break the inheritance link? Add the new keyword.