Sunday, July 1, 2012

Enumerations - New Enum Types


Under this blog post we'll be discussing on " What are Enumerations ?" & creating and using new Enum types.
  •  What are Enumerations ???
An Enumeration type is a set of related named constants. An Enumeration type is a scalar type that has a user defined range of values. Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. The following examples will further explain these to you.
  • Initializing & Using Enums
enum Days
{ Monday, Tuesday = 1, Wednesday, Thursday, Friday,
Saturday, Sunday /* In this case, the enumeration literals Wednesday,
Thursday,Friday,Saturday and Sunday will automatically 
have the values 2, 3,4,5 and 6. */ }; static void Main(string[] args) {
// loops through the days of the week for (Days dayOfWeek = Days.Monday; dayOfWeek <= Days.Sunday;
dayOfWeek++) { Console.WriteLine(dayOfWeek); } Console.ReadLine(); //Output is: //Monday //Tuesday //Wednesday … }

No comments:

Post a Comment