Enumerations is a special group of designated attributes, which are usually integer in the set of all map numbers, when you want to choose between a group of constant values ​​and make a selection with every possible value related to any number, its A wide range of can be used. As you will see in our example, the Enumerations is defined above the sections within our namespace. This means that we can use the Enumerations from all the classes within the same namespace.
Here is an example of a simple calculation that they are all about
public enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }
All of these possible values ​​correspond to a number, if we do not specifically set them, the first value is equal to 0, the next 1 to 1 and so on. The following sections of the code will prove this, as well as how we use one of the following possible values from the enum:
using System;
namespace ConsoleApplication1
{
public enum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }
class Program
{
static void Main(string[] args)
{
Days day = Days.Monday;
Console.WriteLine((int)day);
Console.ReadLine();
}
}
}
The output will be zero, because Monday's price map is directly at number zero. Obviously we can change this - change the line in some way like this:
public enum Days { Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }
If you run your code again, you will see that Monday is now equal to 1 equals 1. All other values ​​will have results as well. You can assign other values ​​with other values. Due to the direct mapping of a number, you can use numbers to get the value related to the Enumerations , such as:
Days day = (Days)5;
Console.WriteLine(day);
Console.ReadLine();
Another gofeature of enumerations is the fact that you can a string representation of the values as well. Change the above example to something like this:
static void Main(string[] args)
{
string[] values = Enum.GetNames(typeof(Days));
foreach(string s in values)
Console.WriteLine(s);
Console.ReadLine();
}
The Enum class contains a bunch of useful methods for working with enumerations.