简体   繁体   中英

Should I use an Enum class or a Lookup in c#

Here is my ultimate goal: I want to have a way of outputting tool names based on whatever integer is registered in the program. For instance, if integers, 1001, 1002, and 1003, where found by the program, then the program would tell the user that Tool 1, Tool 2, and Tool 3 were found.

I have gotten this to work using a simple enum class where all of the "tools" are numbered and correspond to the correct integer. I can even output the enum as a name, although I would like to output a string, preferably one that can be edited easily without having to change the entire program too much. Like it would be great if I could program it all as Tool 1, Tool 3, and Tool 7 now, and then go back and just change the strings when I actually know the tools that I am using.

I have also already tried to use the DescriptionAttribute with this enum, but even with the help of a few questions already, I was not able to figure out how to look up the enum by the integer and then output the Description of that Enum. I could do either or, but not both, simply.

So that led me to wonder if there was a better way that I could handle all of this besides using the Enum class, which I have heard is not as useful in c#. I just want to know any suggestions on how I can do this with an enum class, or other methods within c# that I can use to achieve this.

Just let me know if I need to share my working code that I already have. Thanks!

I would recommend having a tool class with a few properties;

public class Tool
{
    public string Name { get; set; }
    public string Description { get; set; }
}

Then in the code that deals with these I would use a Dictionary<int, Tool> . Then if say you wanted the description for tool 1001 you would just do:

  Console.WriteLine(myTools[1001].Description);

Of course you could just use a Dictionary<int, string> but you'll probably find later on that you'll want more data associated with the tool so creating a class makes more sense.

Sounds like a good case for a Dictionary

Example:

var tools = new Dictionary<int, string>
{
    {1001, "Tool 1"},
    {1002, "Tool 2"},
    {1003, "Tool 3"},
};

string tool1Name = tools[1001];
//toolName == "Tool 1"

Enums are very useful. There is a time and place for them and in your example, you really shouldn't use them. They represent a constant value in a strongly-typed fashion and can be used as flags if you mark them so.

For your situation, a class would work.

public class ToolItem
{
    public int Id { get; set; }
    public string Description { get; set; }
    public override ToString() { //...Your logic }
}

You could then use a List<T> . Dictionary<TKey, TValue> , etc.. to hold a reference to them and use LINQ to work with the collections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM