How can I cast int to enum?

后端 未结 30 1473
礼貌的吻别
礼貌的吻别 2020-11-22 00:56

How can an int be cast to an enum in C#?

30条回答
  •  北海茫月
    2020-11-22 01:07

    The following is a slightly better extension method:

    public static string ToEnumString(this int enumValue)
    {
        var enumString = enumValue.ToString();
        if (Enum.IsDefined(typeof(TEnum), enumValue))
        {
            enumString = ((TEnum) Enum.ToObject(typeof (TEnum), enumValue)).ToString();
        }
        return enumString;
    }
    

提交回复
热议问题