Static Members - Guess the Output

public class ABC
{
    public static string Description
    {
        get { return "Class ABC implemented"; }
    }
    public string GetDescription()
    {
        return Description;
    }
}

public class BCD : ABC
{
    public new static string Description
    {
        get { return "Class BCD implemented"; }
    }
}

class program
{
    static void Main(string[] args)
    {
        Console.WriteLine(new BCD().GetDescription());
        Console.ReadKey();
    }
}


Option 1: Class ABC implemented
Option 2: Class BCD implemented
Option 3: Error

Answer: Option 1: Class ABC implemented

Explanation: Static members are relevant to types and not to instances. 

In this case, return Description; always returns the value of Description property of class ABC type.

No comments:

Post a Comment