What is the usage of ENUM in .NET?

In your coding, you will represent constant values using the keyword const. But this keyword can define only one constant. If you want to specify set of related constants then you can use enum. The enum is a value type containing set of constants that are related to one another. The enum can hold values of any integral type except the type char. Here is an example:



class sampleSeasons {
public enum Spring {
March, April, May
}
public enum Summer {
June, July, August
}
public enum Autumn {
September, October, November
}
public enum Winter {
December, January, February
}
public static void Main() {
Console.WriteLine("Spring Season occurs in following months:");
foreach (string month in Enum.GetNames(typeof(Spring))) {
Console.WriteLine("Month: {0} Value: {1}", month,
(int)Enum.Parse(typeof(Spring), month));
}
Console.WriteLine("Summer Season occurs in following months:");
foreach (string month in Enum.GetNames(typeof(Summer))) {
Console.WriteLine("Month: {0} Value: {1}", month,
(int)Enum.Parse(typeof(Summer), month));
}
Console.WriteLine("Autumn Season occurs in following months:");
foreach (string month in Enum.GetNames(typeof(Autumn))) {
Console.WriteLine("Month: {0} Value: {1}", month,
(int)Enum.Parse(typeof(Autumn), month));
}
Console.WriteLine("Winter Season occurs in following months:");
foreach (string month in Enum.GetNames(typeof(Winter))) {
Console.WriteLine("Month: {0} Value: {1}", month,
(int)Enum.Parse(typeof(Winter), month));
Console.ReadLine();
}
}

Output of this code will be:

Spring Season occurs in following months:
Month: March Value: 0
Month: April Value:1
Month:May Value:2
Summer Season occurs in following months:
Month: June Value: 0
Month: July Value: 1
Month: August Value: 2
Autumn Season occurs in following months:
Month: September Value: 0
Month: October Value:1
Month: November Value:2
Winter Season occurs in following months:
Month: December Value: 0
Month: January Value:1
Month: February Value:2

In this example, you have defined four seasons as four enumerations each containing the list of months when the season will occur. In this example, note the following:

• While defining the enumerations, you have not specified any values for the enum constants. But in the output you can see that the values are 0, 1, 2 for each of the constant in each enumeration. How is this value assigned? By default the first constant in the enum will have the value 0, the next value will be 1+ the previous constant and so on. However C# provides you an option to modify the default values. If you want to display January as 1, February as 2, March as 3 and so on in the order of months in an year then you can change the enumerations as shown below:
public enum Spring {
March=3, April, May
}
public enum Summer {
June=6, July, August
}
public enum Autumn {
September=9, October, November
}
public enum Winter {
December=12, January=1, February
}

Now the output of the code will be:

Spring Season occurs in following months:
Month: March Value: 3
Month: April Value:4
Month:May Value:5
Summer Season occurs in following months:
Month: June Value: 6
Month: July Value: 7
Month: August Value: 8
Autumn Season occurs in following months:
Month: September Value: 9
Month: October Value: 10
Month: November Value: 11
Winter Season occurs in following months:
Month: December Value: 12
Month: January Value: 1
Month: February Value:2

• In this example, you have not defined any type for the enum. Hence it is assumed as int by default. However you are allowed to specify type of an enum as byte or sbyte or short or ushort or int or uint or long or ulong. Here is an example showing enum of type byte:
public enum Spring:byte { … }

• Note that values of enum constants should be explicitly casted before assigning to integral types. In the above example, you have explicitly casted the value to int since the enum is of type int. The corresponding code is shown below:
Console.WriteLine("Month: {0} Value: {1}", month, (int)Enum.Parse(typeof(Spring), month));

| What is Private Access Modifier in C#? |Introducing Microsoft SQL Server 2000 Desktop Engine | What is Protected Access Modifier in C#? | What is Protected Internal Access Modifier in C#? | What is Public Access Modifier in C#? | What is the difference between virtual and abstract keywords in .NET? | What is the importance of Microsoft Application Blocks in .NET Architecture? | What is the need for Factory Method in C# | What is the purpose of ArrayList in .NET? | What is the purpose of Datareader in ADO.NET? | What is the purpose of Dataset in ADO.NET? | What is the purpose of finally block in C#? | What is the purpose of interlocked class in .NET? | What is the purpose of main() function in C# | What is the purpose of ManualResetEvent in .NET? | What is the purpose of sealed method in C#? | What is the purpose of Thread.Join() method in .NET? | What is the purpose of Thread.Sleep() method in .NET? | What is the purpose of throw keyword in C#? | What is the usage of ENUM in .NET? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.