Method Hiding

noun

say it: “METH-ud HY-ding” /ˈmɛθəd ˈhaɪdɪŋ/

A derived class method with the same name as an inherited one, not marked override, which runs only through the derived type.

What is Method Hiding in C#?

Method Hiding — A derived class declaring a method with the same name as an inherited one, without override. The new method only runs through a variable of the derived type; through a base class variable, the base version runs. Mark it new to say you meant it.

Book item = new Ebook();
Console.WriteLine(item.Describe()); // A book

class Book { public string Describe() => "A book"; }
class Ebook : Book { public new string Describe() => "An ebook"; }

In a sentence

  • “Because of method hiding, calling Describe() through a Book variable still said "A book".”

Level: Journeyman · More C# terms at this level

Updated