acermate433s
acermate433s

Reputation: 2544

How can you override an enum in C#?

This is my code:

void Main()
{
    StarTrek baseClass = new StarTrek();
    NewGeneration childClass = new NewGeneration();

    baseClass.ShowEnum();
    childClass.ShowEnum();
}

public class StarTrek
{
    internal enum Characters
    {
        Kirk, Spock, Sulu, Scott
    }

    public void ShowEnum()
    {
        Type reflectedEnum = typeof(Characters);
        IEnumerable<string> members = reflectedEnum.GetFields()
                                            .ToList()
                                            .Where(item => item.IsSpecialName == false)
                                            .Select(item => item.Name);
        string.Join(", ", members).Dump();
    }
}

public class NewGeneration : StarTrek
{
    internal new enum Characters
    {
        Picard, Riker, Worf, Geordi
    }       
}

ShowEnum always displays:

Kirk, Spock, Sulu, Scott

even if it was called in the NewGeneration class. Am I missing/misunderstanding something? Is there a way for ShowEnum to use the NewGeneration.Characters instead of StarTrek.Characters?

Upvotes: 5

Views: 8357

Answers (4)

Usman
Usman

Reputation: 69

class Program
{
    public static void Main()
    {
        StarTrek baseClass = new StarTrek();
        NewGeneration childClass = new NewGeneration();

        baseClass.ShowEnum();
        childClass.ShowEnum();
        Console.ReadLine();
    }
}


public class StarTrek
{
    internal enum Characters
    {
        Kirk, Spock, Sulu, Scott
    }

    public virtual void ShowEnum()
    {
        Type reflectedEnum = typeof(Characters);
        IEnumerable<string> members = reflectedEnum.GetFields()
                                            .ToList()
                                            .Where(item => item.IsSpecialName == false)
                                            .Select(item => item.Name);
        Console.WriteLine(string.Join(", ", members.ToArray()));
    }
}

public class NewGeneration : StarTrek
{
    internal new enum Characters
    {
        Picard, Riker, Worf, Geordi
    }

    public override void ShowEnum()
    {
        Type reflectedEnum = typeof(Characters);
        IEnumerable<string> members = reflectedEnum.GetFields()
                                            .ToList()
                                            .Where(item => item.IsSpecialName == false)
                                            .Select(item => item.Name);
        Console.WriteLine(string.Join(", ", members.ToArray()));

    }
}

Upvotes: 1

Firo
Firo

Reputation: 30813

public class StarTrek
{
    internal virtual IList<string> Characters
    {
        get
        {
            return new List<string> { Kirk, Spock, Sulu, Scott };
        }
    }

    public void ShowEnum()
    {
        string.Join(", ", Characters).Dump();
    }
}

public class NewGeneration : StarTrek
{
    internal override IList<string> Characters
    {
        get
        {
            return new List<string> { Picard, Riker, Worf, Geordi };
        }
    }
}

Upvotes: 1

Oded
Oded

Reputation: 499012

You can't overload enums - they are not classes or structs.

With internal new you are simply hiding the original property, though, since you are ShowEnum is defined on the base class, the base class implementation of the property is called.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500675

Your second enum is shadowing the first one, in just the same way as declaring a new field within NewGeneration would. Nested types aren't polymorphic - the code in ShowEnum will always refer to StarTrek.Characters; the code is built against that type at compile-time.

It's not really clear what you're trying to do, but you definitely can't do it in the way that you're trying. If you want polymorphic behaviour you have to be using methods, properties or events, which must then be declared virtual in order to be overridden in the derived class.

(It's worth noting that overloading and overriding are different, by the way - your question refers to overloading but I think you meant overriding.)

Upvotes: 6

Related Questions