It’s hard to find a developer who has never heard about enum. But do you use it correctly? Let’s find out.

Correct naming

The naming is one of the important things in development. Jokes aside. The best name for enum should reflect what it consists of. The good names are short and self-explanatory:

WindowName,EntityType, etc. It’s that simple. There is no reason to add ‘s’, or ‘enum’ or call it ListOfEntitiesType. Brevity is the soul of wit. And don’t forget to use a namespace, so your enum will be separated by them.

What can be the values of the enum members?

By default, it’s int, and I recommend sticking with it. But sometimes, there is a requirement to do something different, and for the value of enum members, you can use any integral numeric type you like: bytes, int’s, long’s, and even shorts.

Correct indexing

Many developers use invalid indexes as default. For example,

-1orint.MinValue, or-99999. These things are annoying. Let’s look at the example: you have an enumDamageTypeand classDamage, which you send somewhere to draw some damage.

enum DamageType 
{
    None = -1,
    Range = 0,
    Magic = 1,
    Close = 2,
}

class Damage 
{
    public DamageType Type { get; private set; }
    public int Amount { get; private set; }
    …..
}

If you create this class with an empty constructor, you will get these default values: Type is

DamageType.Range, and Amount is0. This is not logical since the default ofDamageTypewill be None unless your goal is different.

#c-sharp #software-development #enum #enum in c# #c#

How to Use Enum in C#
1.10 GEEK