ENUM IN C#
Part-1 Enum In C#
Part-2 Enum In C#

In This Blog, you are going to learn the most important topic of csharp programming languages that is Enum (Enumerations) In Csharp. You can find all the notes and source code from the bottom of this blog.

Objectives

  • What is Enum?
  • Built In Enums.
  • How Can We Create Enums?
  • Practical Example
  • Why And When To Use Enums?
  • Enum Conversion (Type Casting With Enum)
  • Enum With Switch (Pratical Example)

C# Enum

  • Enum is a set of Constants.
  • In C#, an enum is a special “class” that represents a group of constants (unchangeable/read-only variables)
  • In C#, Enum is short for “enumerations”, which means “specifically listed”.

Enum In C#

  • In C#, an enum (or enumeration type) is used to assign constant names to a group of numeric integer values.
  • It makes constant values more readable, for example, WeekDays.Monday is more readable then number 0 when referring to the day in a week.
  • In C#, an enum is defined using the enum keyword, directly inside a namespace, class, or structure. All the constant names can be declared inside the curly brackets and separated by a comma.
  • In C#, the default underlying type of an enum is int.
  • In C#, the default value for first element is ZERO and gets incremented by 1.
  • In C#, Enums are value types.
  • In C#, Enums are more readable and maintainable.
  • In C#, Enum is converted into abstract class behind the scenes.

How to create Enum in c#

In C#, To create an enum, use the enum keyword (instead of class or interface), and separate the enum items with a comma.

C# Enum Values

  • In C#, If values are not assigned to enum members, then the compiler will assign integer values to each member starting with zero by default.
  • In C#, The first member of an enum will be 0, and the value of each successive enum member is increased by 1.
  • In C#, You can assign different values to enum member.
  • A change in the default value of an enum member will automatically assign incremental values to the other members sequentially.

Enum Conversion

In C#, Explicit casting is required to convert from an enum type to its underlying integral type.

Why And When To Use Enums ?

In C#, Use enums when you have values that you know aren’t going to change, like month days, days, colors etc.

ENUM WITH SWITCH

In C# switch can act upon enum values.

Complete Source Code Of ENUM In C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CsharpEnums
{
    enum Days
    {
        Sunday = 1, //1
        Monday,//2
        Tuesday,//3
        Wednesday,//4
        Thursday,//5
        Friday,//6
        Saturday //7
    }

    class Program
    {
        static void Main(string[] args)
        {

            //Days myDay = Days.Tuesday;
            int myDay = (int)Days.Sunday;
            switch (myDay)
            {
                case 1:
                    Console.WriteLine("Hello This is Sunday");
                    break;
                case 2:
                    Console.WriteLine("Hello This is Monday");
                    break;
                case 3:
                    Console.WriteLine("Hello This is Tuesday");
                    break;
                case 4:
                    Console.WriteLine("Hello This is Wednesday");
                    break;
                case 5:
                    Console.WriteLine("Hello This is Thursday");
                    break;
                case 6:
                    Console.WriteLine("Hello This is Friday");
                    break;
                case 7:
                    Console.WriteLine("Hello This is Saturday");
                    break;
                default:
                    Console.WriteLine("Invalid Day..");
                    break;
            }



            //switch (myDay)
            //{
            //    case Days.Sunday:
            //        Console.WriteLine("Hello This is Sunday");
            //        break;
            //    case Days.Monday:
            //        Console.WriteLine("Hello This is Monday");
            //        break;
            //    case Days.Tuesday:
            //        Console.WriteLine("Hello This is Tuesday");
            //        break;
            //    case Days.Wednesday:
            //        Console.WriteLine("Hello This is Wednesday");
            //        break;
            //    case Days.Thursday:
            //        Console.WriteLine("Hello This is Thursday");
            //        break;
            //    case Days.Friday:
            //        Console.WriteLine("Hello This is Friday");
            //        break;
            //    case Days.Saturday:
            //        Console.WriteLine("Hello This is Saturday");
            //        break;
            //    default:
            //        Console.WriteLine("Invalid Day..");
            //        break;
            //}

            //int[] values = (int[])Enum.GetValues(typeof(Days));

            //foreach (int value in values)
            //{
            //    Console.WriteLine(value);
            //}

            //string[] members = (string[])Enum.GetNames(typeof(Days));

            //foreach (string member in members)
            //{
            //    Console.WriteLine(member);
            //}

            //Console.WriteLine("Enter Your Name");
            //string name = Console.ReadLine();

            //Console.WriteLine("Enter Your Day Sunday = 1, Monday = 2, Tuesday = 3");
            //int value = int.Parse(Console.ReadLine());

            //Days myDay = (Days)value;
            //Console.WriteLine("My Name Is: " + name + " And My Birth Day Is: " + myDay);

            //int value = (int)Days.Saturday;
            //Console.WriteLine(value);

            //Days myDay = (Days)5;
            //Console.WriteLine(myDay);

            //Days birthDay = Days.Friday;
            //Console.WriteLine(birthDay);

            //Console.WriteLine(Days.Sunday);

            //Console.BackgroundColor = ConsoleColor.Yellow;
            //Console.ForegroundColor = ConsoleColor.Red;
            //Console.WriteLine("My Name Is Mohammad Adil");
            Console.ReadLine();
        }
    }
}

Download Notes & Source Code From Link Given Below

https://www.mediafire.com/file/4ygrjgnrah5ki17/ENUM+IN+CSHARP.rar/file

No responses yet

Leave a Reply

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