C#: Accessing base class’ hidden members

This entry is part 55 of 71 in the series C#

The base keyword has another purpose in life.  It can be used to call a hidden member or an overridden method in a base class.

Since base represents an instance of the direct parent, base.member returns the specified member in the parent class.

You can access any member in the base class that is visible from the derived class.

using System;

namespace HiddenMember
{
    class Program
    {
        public class Star
        {
            public string Identify()
            {
                return "Hidden Identity";
            }
        }

        public class Actress : Star
        {
            public new string Identify()
            {
                return "New Identity";
            }

            public void Write()
            {
                Console.WriteLine(base.Identify());
                Console.WriteLine(Identify());
            }
        }

        static void Main()
        {
            Actress actress = new Actress();
            actress.Write();
            Console.ReadKey();
        }
    }
}

The Actress class declares another method Identify using new.  Without the base keyword, using the actress instance will access the Identify method in the derived class.

To access the parent method, we can use the base keyword such as

base.Identify()

The output from running the above code will be

hiddenmembers

Hidden Identity
New Identity

Series Navigation<< C#: The base keyword in constructor
C#: The is Keyword >>