String representation of an Enum

后端 未结 30 2532
不思量自难忘°
不思量自难忘° 2020-11-22 02:44

I have the following enumeration:

public enum AuthenticationMethod
{
    FORMS = 1,
    WINDOWSAUTHENTICATION = 2,
    SINGLESIGNON = 3
}

T

30条回答
  •  面向向阳花
    2020-11-22 03:31

    Very simple solution to this with .Net 4.0 and above. No other code is needed.

    public enum MyStatus
    {
        Active = 1,
        Archived = 2
    }
    

    To get the string about just use:

    MyStatus.Active.ToString("f");
    

    or

    MyStatus.Archived.ToString("f");`
    

    The value will be "Active" or "Archived".

    To see the different string formats (the "f" from above) when calling Enum.ToString see this Enumeration Format Strings page

提交回复
热议问题