Software

What Is C# Enum

Rate this post

Enum is a structure that we need to use if we need to work with constant values.

String expressions are made for numeric matching or different operations to be readable in the software language.

The word enum comes from the word enumerations.

In short, it allows us to define and use the numbers we have determined with string expressions. It starts with the enum keyword and then a name is given in the continuation, then the fancy paranetesis opens.

Enum Season {Spring, Summer, Autumn, Winter}

 

The seasons are always the same and do not change. In other words, it is constant.For this reason, when we need to use the seasons, we can use it with enum.

Enum is a structure that holds a numerical value.

 

C# Number Examples

enum Days {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

};



string[] x = Days.GetNames(typeof(Days);
MessageBox.Show(x[0].ToString());
MessageBox.Show(Days.GetNames(typeof(Days))[3]);

In this example, by assigning an array of enum values, we can find the day by index order.

1. Output of MassageBox:

Monday 

2. Output of MassageBox:

Thursday

Things To Know When Using An Enum

  • If we do not value the data in the enum, the values ​​increase by one starting from 0.
  • The default value in Enum is “int”.
  • Enums; We can create byte, sbyte, short, ushort, int, uint,long, ulong types.
  • The rules we pay attention to in variable naming apply to the values ​​we give in the enum.For example, we cannot give names that start with a number or contain spaces.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button