Contents
Should I use enum or String?
Using an enum means you can only have one of those three values or it won’t compile. Using strings means your code has to constantly check for equality during runtime – so it becomes bloated, slower and error prone. Partly, it is a mater of conciseness of communication. Strings are general things, enums are specific.
Does dictionary key need String?
Second, a dictionary key must be of a type that is immutable. For example, you can use an integer, float, string, or Boolean as a dictionary key. However, neither a list nor another dictionary can serve as a dictionary key, because lists and dictionaries are mutable.
Why are enums better than strings?
I would consider Enums to be a better approach than Strings. They are type safe and comparing them is faster than comparing Strings. If your set of parameters is limited and known at compile time, use enum . If your set of parameters is open and unkown at compile time, use strings.
Are enums faster than strings?
Enum. values() gives you a reference to an array, and iterating over an array of enums costs the same as iterating over an array of strings. Meanwhile, comparing enum values to other enum values can actually be faster that comparing strings to strings.
Can enum return string C#?
The enum class is used to declare an enumeration in C#. By default, the type of variables inside the enum class is int. Then we created the extension function GetString() that uses a switch() statement to determine the value of the enumeration and return a string according to the value.
When to use strings or an ENUM for dictionary keys?
Make sure everybody uses the values in a form MyEnum.SomeValue, and not in a form of underlying integers. If somebody uses integers, the risk is that when changing the order of the values within the enum or adding the values in the middle of it or removing values will change the match between some values and the underlying numbers.
When to use an enum instead of a toString?
Make sure you use an explicit map instead of a simple ToString. Renaming a value within the enum is a simple refactoring task and shouldn’t break the code. If the string value matches the actual value name within the enum, the code will break in a subtle way which wouldn’t be easy to debug.
Is it good practice to change enum in C #?
Renaming a value within the enum is a simple refactoring task and shouldn’t break the code. If the string value matches the actual value name within the enum, the code will break in a subtle way which wouldn’t be easy to debug. See also: Is this a Best Practice with Enum in C# and the comments by MK87.
Which is the best way to pass enum params?
If you want the user to be able to easily identify the url then use strings. If not there is no reason not to use integers. As you said, you can use FlagsAttribute to combine multiple values. Thanks for contributing an answer to Stack Overflow!