|
|
If you are using .NET 4.0, then Enum.TryParse<TEnum> is now provided out of the box and support flags enumeration. As given in the documentation, Enum.TryParse<> ‘Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. The return value indicates whether the conversion succeeded.’
Here’s how to use it:
[Flags]
enum Result { Fail = 0, Pass = 1, Grace = 2 };
static void Main(string[] args)
{
string a = (Result.Pass || Result.Grace).ToString();
Result b;
bool success = Enum.TryParse<Result>(a, out b);
Console.WriteLine("{0} = {1}", success, b);
Console.ReadLine();
}
OUTPUT

'Like' us on our FaceBook page if you find this blog useful. Thanks!
Did you like this post?
|
|
|
||
|
|
|
|
Save on Delicious |
|
|
subscribe via rss |
|
subscribe via e-mail |
|
|
print this post |
|
follow me on twitter |





comments
3 Responses to "Enum.TryParse in .NET 4.0"Nice post but I think a pipeline between Result.Pass Result.Grace is missed. Am I right?
Thanks! yes the or symbol was missing..fixed it
its trick with Flags Attribute
Post a Comment