Lesson 28 Learning to work with Enumeration in C#

Enumeration are a collection of named values that correspond to a set of numbers, and they are often of the integer type. Enumeration are useful in a variety of situations, such as when you need to quickly select a value from a set of fixed values and match it to a numeric value.

Enumeration are defined above the classes and inside the namespace, as seen in the practical examples code in this lecture. As a result, instead of the programme, you can use any Enumeration in the entire namespace.

The following code shows a simple example of defining Enumerations in C#: public enum Days {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}.

All of the values in the Enumeration above have a numerical value. If the associated values are not explicitly set enumerations, the first value corresponds to o, the second value corresponds to 1, and so on.

The following code shows how to connect each of the Enumeration items by default and shows you how to use one Teaches a specific value in terms of an Enumeration:

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;

The output of the example code above will be zero, because the Monday value corresponds directly to the o value. Of course by The following code can change the number corresponding to the Enumerations to any value: public enum Days {Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}
If you run the above code, you will see that this time Monday will correspond to one instead of zero.

Other values Enumeration will each have a value greater than 1, respectively. Tip: You can assign each Enumeration member to a specific number and not necessarily in a row.

Due to the direct relationship of each Enumeration member with its corresponding number, you can count the numbers of each member Read your desired enumeration. As the following code:

Days day = (Days) 5;
Console.WriteLine (day);
Console.ReadLine ();
Another feature of Enumerations is that with iterative loops, you can string values
Show an Enumeration in the output. Change the code of the previous example to the following code:
static void Main (string [] args)
  string [] values = Enum.GetNames (typeof (Days));
  foreach (string s in values)
  Console.WriteLine (s);

  Console.ReadLine ();
Class Enum has a set of application methods that you can use to work with Enumeration

About Author:

My name is Wajahat Hussain Founder of bestinformativeblog. I welcome you to the Operating System of Blog – bestinformativeblog. I Am An Blogger By Passion. Well experienced for more than 4+ years in SEO, WordPress, Blogging and how to make Passive income, Explore about me by clicking below.