Enumerations and String Values ​​in ASP.NET

I am looking for list recommendations and getting a related string value. Considering this:

public enum Fruits {
    Apple,
    Orange,
    Grapefruit,
    Melon
}

What is the best way to get the associated string value of a name? For instance. "Grapefruit", given that the string value may not correspond to the representation in the listing. for example, "Kasab melon"

My current thinking is a function that takes an enumeration and returns a string, but that does not mean hard coding string values ​​(which I prefer not to do)? Is using a resource file in which a string can be retrieved through an enumeration too heavy?

+3
source share
2 answers

, , .

, - , , ( , ).

+4

R0MANARMY . , , . , .

,

public enum NicePeople
{
    SomeGuy,
    SomeOtherGuy
}

,

public static class MyExtensions
{
    public static string GetName(this NicePeople tst)
    {
        switch (tst)
        {
            case NicePeople.SomeGuy:
                return "Some Nice guy";
            case NicePeople.SomeOtherGuy:
                return "Another Nice Guy";
            default:
                throw new Exception("Naw");
        }
    }
}

,

NicePeople.SomeGuy.GetName()
+1

Source: https://habr.com/ru/post/1746708/


All Articles