4

I'm working with the Umbraco CMS which holds lots of data as strings.

Sometimes I need to compare a stored value string value (which is an int stored as a string) to an enum, but is it best to compare them as strings:

if ( stringValue == ( (int) Enum.Option ).ToString() ){
}

Or to parse and compare as ints:

if ( int.Parse(stringValue) == (int) Enum.Option ){
}

Or does it just not matter either way!

Peter Bridger
  • 9,123
  • 14
  • 57
  • 89

6 Answers6

4

You should compare data in its native/canonical form. So use integers. Performance is usually a second-order concern in such cases. Correctness is first.

usr
  • 168,620
  • 35
  • 240
  • 369
3

Maybe you want to try to use Enum.Parse?

enum MyEnum
{ 
  Option,
  Option1 = 1, 
  Option2 = 2
}

string stringValue = "0";
if((MyEnum)Enum.Parse(typeof(MyEnum), stringValue) == MyEnum.Option)
{
   //Do what you need
}

Note:

The value parameter contains the string representation of an enumeration member's underlying value or named constant, or a list of named constants delimited by commas (,).

So stringValue can be "Option" or "0".

Renatas M.
  • 11,694
  • 1
  • 43
  • 62
  • The string is "an int stored as a string", so parsing that string as an `enum` is going to fail. – Sergey Kalinichenko Jul 30 '12 at 11:20
  • @dasblinkenlight Haven't tested but I believe you are wrong. Remarks first line: `The value parameter contains the string representation of an enumeration member's underlying value or named constant, or a list of named constants delimited by commas (,).` – Renatas M. Jul 30 '12 at 11:29
  • Also nice trick: `If value is a list, the return value is the value of the specified names combined with a bitwise OR operation.` – Renatas M. Jul 30 '12 at 11:37
  • You may want to mention the ability to pass numeric strings representing enum values to the "Parse" method. I am sure it would be new to many people, even if they know of the existence of the `Parse` method. – Sergey Kalinichenko Jul 30 '12 at 11:40
  • Try also generic version `Enum.TryParse(string, out T)` – abatishchev Jul 30 '12 at 11:56
0

Its even better if you will compare enums.

Uriil
  • 11,948
  • 11
  • 47
  • 68
0

For the sake of code readability, I'd choose the second approach: it makes clear beyond doubt that your string is expected to contain an integer in that particular context, and you're treating it as such.

Second approach would also allow you to handle error cases more deeply (what if your string isn't an integer ? Second block would throw, first one would silently act just like your data was different from the enum).

Also, as already stated, comparing integers is always better performance-wise than comparing strings, but I believe there wouldn't be much real-world difference in this case.

Alex
  • 23,004
  • 4
  • 39
  • 73
0

Casting from int to an enum is extremely cheap... it'll be faster than a dictionary lookup. Basically it's a no-op, just copying the bits into a location with a different notional type.

Parsing a string into an enum value will be somewhat slower.

from this SO answer.

Community
  • 1
  • 1
Ria
  • 10,237
  • 3
  • 33
  • 60
0

If you want to check the validity, you can use

int value;
Option option;


if (int.TryParse(stringValue, out value) &&
    Enum.IsDefined(typeof(Option), value)) { 
    option=(Option)value;
}
M. Mennan Kara
  • 10,072
  • 2
  • 35
  • 39