C# Enum

Enum is a value type which can be byte, short, int and long. We can use enum to avoid the occurrence of magic number. For example:

        var season = dateSeason == 1 ? “Spring” : “other season”

you might notice that when the project grows bigger, there will be more magic numbers you generate. This may create more Bugs the programmer produces and makes the project’s overall maintenance more difficult. We can use enum to avoid this kind of situation.

Make sure 0 is still available status while using enum The default value of enum is zero. You have to make sure that 0 is always at an available status when you use it. For example, if you make your enum variable starts from 1,  in some cases when the other programmer tries to use your enum variable you declared, they don’t know 0 is not an expected value and this could make some unexpected things happen.

The result returns 0 which is not anticipated and this could make some unwanted scenarios happen. Hence the best thing is to make sure that 0 is always the expected and available status when you use it.

Final conclusion

By using enum, we can avoid the occurrence of magic numbers and this thereby increases the code’s readability and maintenance. Enum is an useful way to enhance the quality of code if you use this value type correctly.