Search for a string in Enum and return the Enum

后端 未结 12 2206
礼貌的吻别
礼貌的吻别 2020-11-28 19:13

I have an enumeration:

public enum MyColours
{
    Red,
    Green,
    Blue,
    Yellow,
    Fuchsia,
    Aqua,
    Orange
}

and I have a s

12条回答
  •  遥遥无期
    2020-11-28 20:00

    As mentioned in previous answers, you can cast directly to the underlying datatype (int -> enum type) or parse (string -> enum type).

    but beware - there is no .TryParse for enums, so you WILL need a try/catch block around the parse to catch failures.

提交回复
热议问题