In this lesson, you will learn about enum in C# with examples, and when to use them.

C# Enum

Enum or Enumeration is a set of integer constants that are used to assign name or string value. An example of is the days of the week, where the name can be ‘Days’, and the enumerators are the 7 days of the weeks. Enum are used to make coding easy and maintainable. Also, use enum for set of values that they will not change, Such as day of the weeks, months, genders, etc…

Enum syntax

Below is the enum syntax in C# 

enum <enum_name> { enumeration list }; 

Example of Enum in C#

  enum Gender
        { Male, Female };

static void Main(string[] args) 

        { 

            Console.Write("I'm a " + Gender.Male); 

            Console.ReadKey(); 

        } 

Another example of enum in C#

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


        static void Main(string[] args)
        {
            string Todayis;
            Todayis = "Friday";
            if (Todayis == Days.Saturday.ToString())
            {
                Console.Write("Today is a weekend");
            }
            else
            {
                Console.Write("Today is a weekday");
            }

            Console.ReadKey();
        }

 

Last modified: January 30, 2019

Comments

Write a Reply or Comment

Your email address will not be published.