0

I have a very basic question. In Java, it is possible to point attributes and variables to Enums, such as:

public enum DayTime{
    Morning("Morning"),
    Afternoon("Afternoon"),
    Night("Night");

    private string description;

    Daytime(string description){
        this.description = description;
    }

    public string getDescription(){
        return description;
    }
}

Is it possible to apply the same concept to C#? I am trying to get modular descriptions to products, whereas their name, contents and characteristics would be shown in a string of text, and Enums looked like the best alternative to modify this text according to which characteristic is selected.

marstran
  • 26,413
  • 5
  • 61
  • 67
JoeKalango
  • 27
  • 4
  • @OmairMajid no, but it is an interesting take. – JoeKalango May 10 '20 at 18:26
  • 1
    Does this answer your question? [C# vs Java Enum (for those new to C#)](https://stackoverflow.com/questions/469287/c-sharp-vs-java-enum-for-those-new-to-c) – Savior May 10 '20 at 18:37
  • Bit of a bad example code. First of all, you should use all uppercase for your enum constants. Secondly, `name()` already returns the name of the constant, so you'd just have to change the case a bit. – Maarten Bodewes May 10 '20 at 23:20

1 Answers1

1

C# enums are very basic compared to Java enums. If you want to simulate the same kind of behavior you need to use a class with an inner enum:

using System.Collections.Generic;

public sealed class DayTime
{
    public static readonly DayTime Morning = new DayTime("Morning", InnerEnum.Morning);
    public static readonly DayTime Afternoon = new DayTime("Afternoon", InnerEnum.Afternoon);
    public static readonly DayTime Night = new DayTime("Night", InnerEnum.Night);

    private static readonly List<DayTime> valueList = new List<DayTime>();

    static DayTime()
    {
        valueList.Add(Morning);
        valueList.Add(Afternoon);
        valueList.Add(Night);
    }

    //the inner enum needs to be public for use in 'switch' blocks:
    public enum InnerEnum
    {
        Morning,
        Afternoon,
        Night
    }

    public readonly InnerEnum innerEnumValue;
    private readonly string nameValue;
    private readonly int ordinalValue;
    private static int nextOrdinal = 0;

    private string description;

    internal DayTime(string name, InnerEnum innerEnum)
    {
        this.description = name;

        nameValue = name;
        ordinalValue = nextOrdinal++;
        innerEnumValue = innerEnum;
    }

    public string Description
    {
        get
        {
            return description;
        }
    }

    //the following methods reproduce Java built-in enum functionality:

    public static DayTime[] values()
    {
        return valueList.ToArray();
    }

    public int ordinal()
    {
        return ordinalValue;
    }

    public override string ToString()
    {
        return nameValue;
    }

    public static DayTime valueOf(string name)
    {
        foreach (DayTime enumInstance in DayTime.valueList)
        {
            if (enumInstance.nameValue == name)
            {
                return enumInstance;
            }
        }
        throw new System.ArgumentException(name);
    }
}

Given this complexity, it may be best to rewrite your logic in a way that's more natural for C# without using enums.

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28