GST Billing Software

header image

ENUM

Erachana Line

In C#, enum is a value type data type. The enum is used to declare a list of named integer constants. It can be defined using the enum keyword directly inside a namespace, class, or structure.

Declaring enum Variable

The general syntax for declaring an enumeration is -

enum <enum_name> {

enumeration list

}

Where,

  • The enum_name specifies the enumeration type name.
  • The enumeration list is a comma-separated list of identifiers.

Ø By default, the first member of an enum has the value 0 and the value of each successive enum member is increased by 1. For example, in the following enumeration, Monday is 0, Tuesday is 1, Wednesday is 2 and so forth.

Example:

enum WeekDays

{

Monday,

Tuesday,

Wednesday,

Thursday,

Friday,

Saturday,

Sunday

}

Ø An explicit cast is necessary to convert from enum type to an integral type. For example, to get the int value from an enum:

int variable_name = (int)enum_name.enum_value;

Example:

using System;

namespace EnumApplication

{

class EnumProgram

{

enum WeekDays {

Monday,

Tuesday,

Wednesday,

Thursday,

Friday,

Saturday,

Sunday

}

static void Main(string[] args)

{

int WeekdayStart = (int) WeekDays.Monday;

int WeekdayEnd = (int) WeekDays.Friday;

Console.WriteLine("Monday: {0}", WeekdayStart);

Console.WriteLine("Friday: {0}", WeekdayEnd);

Console.ReadKey();

}

}

}

Output:

Monday: 1

Friday: 5

Ø The enum is used to give a name to each constant so that the constant integer can be referred using its name.

using System;

public class Program

{

public static void Main()

{

Console.WriteLine(WeekDays.Thursday);

Console.WriteLine((int)WeekDays.Sunday);

}

}

enum WeekDays

{

Monday = 0,

Tuesday = 1,

Wednesday = 2,

Thursday = 3,

Friday = 4,

Saturday = 5,

Sunday = 9

}

Output:

Thursday

Sunday

A change in the value of the first enum member will automatically assign incremental values to the other members sequentially. For example, changing the value of Monday to 10, will assign 11 to Tuesday, 12 to Wednesday, and so on:

Example:

enum WeekDays
{
    Monday = 10,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}
Console.WriteLine((int)WeekDays.Monday);
Console.WriteLine((int)WeekDays.Friday);

Output:

10

14

Enum methods:

Enum is an abstract class that includes static helper methods to work with enums.

Enum Method

Description

Format

Converts the specified value of enum type to the specified string format.

GetName

Returns the name of the constant of the specified value of specified enum.

GetNames

Returns an array of string name of all the constant of specified enum.

GetValues

Returns an array of the values of all the constants of specified enum.

object Parse(type, string)

Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

bool TryParse(string, out TEnum)

Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. The return value indicates whether the conversion succeeded.

Example for enum Methods :

enum WeekDays
{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
}
Console.WriteLine(Enum.GetName(typeof(WeekDays), 4));
Console.WriteLine("WeekDays constant names:");
foreach (string str in Enum.GetNames(typeof(WeekDays)))
            Console.WriteLine(str);
WeekDays wdEnum;
Enum.TryParse<WeekDays>("1", out wdEnum);
Console.WriteLine(wdEnum);

Output:

Friday 
WeekDays constant names:
Monday 
Tuesday 
Wednesday 
Thursday
Friday
Saturday
Sunday 
Tuesday