Lesson 6.6

C# Enumeration with Programming Example


Objective

What is Enumeration in c#?
Programming Example

What is Enumeration in c#?

Enumeration is used for grouping constant in a single list. It assigns all values a number and can be accessed easily by that number. Enumeration is very important when you work on a team and you have only set of constant variable to use.

How to initialize an enumeration in c# programming

You can initialize enumeration with enum keyword:

enum 
{
Enumeration list
}

Programming Example

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

namespace Enumeration_Statement
{
    class Program
    {
        enum Salary{ High, Medium, Low };
        static void Main(string[] args)
        {
            string JamesSalary, MarkSalary;
            JamesSalary = Salary.High.ToString();
            MarkSalary = Salary.Medium.ToString();

            Console.WriteLine("James Salary is " + JamesSalary);
            Console.WriteLine("Mark Salary is " + MarkSalary);
            Console.ReadKey();
        }
    }
}

Output

James Salary is High
Mark Salary is Medium

__

Summary

In this chapter you have learned what the enumeration is and how to use it in program. In the next chapter you will learn about Structure keyword in c#.

BackNext