Skip to main content
added 624 characters in body
Source Link
Jon Skeet
  • 1.5m
  • 892
  • 9.3k
  • 9.3k

Yes - you can use Enum.Parse (and Enum.TryParse as of .NET 4) to parse a string to an enum value.:

Day day = (Day) Enum.Parse(typeof(Day), "Sun");

or

Day day;
if (Enum.TryParse<Day>("Sun", out day))
{
    // Success!
}

It's still somewhat ugly, mind you - quitethere's a bitcertain amount of casting involved in the first call, and the second wouldn't stop you from trying to parse to any value type.

The final reason I don't really like Parse/TryParse is that they will parse string representations of the numeric values of the enums - so "1" would successfully parse, even though it's clearly not the name of an enum value. That seems a bit clunky to me.

There's a simplernicer (IMO!) approach if you use Unconstrained Melody, a small open source project I wrote to allow generics with enum constraints. Then you'd have:

Day day = Enums.ParseName<Day>("Sun");

or

Day day;
if (Enums.TryParseName<Day>("Sun", out day))
{
    // Success!
}

No casting, and you can't accidentally use it on a non-enum type. (There are various other goodies in the project, of course... description parsing etc.)

Yes - you can use Enum.Parse (and Enum.TryParse as of .NET 4) to parse a string to an enum value.

It's still somewhat ugly, mind you - quite a bit of casting involved. There's a simpler approach if you use Unconstrained Melody, a small open source project I wrote to allow generics with enum constraints. Then you'd have:

Day day = Enums.ParseName<Day>("Sun");

or

Day day;
if (Enums.TryParseName<Day>("Sun", out day))
{
    // Success!
}

No casting, and you can't accidentally use it on a non-enum type.

Yes - you can use Enum.Parse (and Enum.TryParse as of .NET 4) to parse a string to an enum value:

Day day = (Day) Enum.Parse(typeof(Day), "Sun");

or

Day day;
if (Enum.TryParse<Day>("Sun", out day))
{
    // Success!
}

It's still somewhat ugly, mind you - there's a certain amount of casting involved in the first call, and the second wouldn't stop you from trying to parse to any value type.

The final reason I don't really like Parse/TryParse is that they will parse string representations of the numeric values of the enums - so "1" would successfully parse, even though it's clearly not the name of an enum value. That seems a bit clunky to me.

There's a nicer (IMO!) approach if you use Unconstrained Melody, a small open source project I wrote to allow generics with enum constraints. Then you'd have:

Day day = Enums.ParseName<Day>("Sun");

or

Day day;
if (Enums.TryParseName<Day>("Sun", out day))
{
    // Success!
}

No casting, and you can't accidentally use it on a non-enum type. (There are various other goodies in the project, of course... description parsing etc.)

Source Link
Jon Skeet
  • 1.5m
  • 892
  • 9.3k
  • 9.3k

Yes - you can use Enum.Parse (and Enum.TryParse as of .NET 4) to parse a string to an enum value.

It's still somewhat ugly, mind you - quite a bit of casting involved. There's a simpler approach if you use Unconstrained Melody, a small open source project I wrote to allow generics with enum constraints. Then you'd have:

Day day = Enums.ParseName<Day>("Sun");

or

Day day;
if (Enums.TryParseName<Day>("Sun", out day))
{
    // Success!
}

No casting, and you can't accidentally use it on a non-enum type.