Creating an Enumeration
To create an enumeration, you use the enum
keyword, followed by the name of the enumeration, followed by a name for
each item of the list. The name of the enumerator and the name of each item
of the list follows the rules of names in the C# language. The formula of creating an enumeration is:
enum Enumeration_Name {Item1, Item2, Item_n};
To create an enumeration, you can manually type the
code. To use a code snippet, right-click where you want to add the
enumeration and click Insert Snippet... Double-click Visual C#. In the list,
double-click enum:
Here is an example of creating an enumeration:
using System;
public class Exercise
{
enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }
static int Main()
{
return 0;
}
}
Declaring an Enumeration Variable
After creating an enumeration,
each member of the enumeration holds a value of a natural number,
such as 0, 4, 12, 25, etc. In C#, an enumeration cannot hold character
values (of type char).
After creating an enumeration, you can declare a
variable from it. Here is an example:
using System;
public class Exercise
{
enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }
static int Main()
{
HouseType propType;
return 0;
}
}
Just as done with the other types, you can use the var keyword to declare a variable of an enumeration type.
Initializing an Enumeration Variable
After declaring a variable for an enumeration, to
initialize it, specify which member of the enumeration would be assigned to
the variable. You should only assign a known member of the enumeration. To
do this, on the right side of the assignment operator, type the name of the
enumeration, followed by the period operator, and followed by the member
whose value you want to assign. Here is an example:
using System;
public class Exercise
{
enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }
static int Main()
{
var propType = HouseType.SingleFamily;
return 0;
}
}
You can also find out what value the declared variable
is currently holding. For example, you can display it on the console using
Write() or WriteLine(). Here is an example:
using System;
public class Exercise
{
enum HouseType { Unknown, SingleFamily, TownHouse, Condominium }
static int Main()
{
var propType = HouseType.SingleFamily;
Console.WriteLine("House Type: {0}", propType);
return 0;
}
}
This would produce:
House Type: SingleFamily Press any key to continue . . .
An enumeration is in fact a list of numbers where each
member of the list is identified with a name. By default, the first item of
the list has a value of 0, the second has a value of 1, and so on. For
example, on the HouseType enumeration, Unknown has a value of 0 while
Townhouse has a value of 2. These are the default values. If you don't want
these values, you can specify the value of one or each member of the list.
Suppose you want the Unknown member in the above enumeration to have a value
of 5. To do this, use the assignment operator "=" to give the desired value.
The enumerator would be:
using System; public class Exercise { enum HouseType { Unknown = 5, SingleFamily, TownHouse, Condominium } static int Main() { return 0; } }
In this case, Unknown now would have a value of 5,
SingleFamily would have a value of 6 because it follows a member whose value
is 1 (thus 5 + 1 = 6). Townhouse would have a value of 7, and Condominium
would have a value of 8. You can also assign a value to more than one member
of an enumeration. Here is an example:
using System; public class Exercise { enum HouseType { Unknown = 3, SingleFamily = 12, TownHouse, Condominium = 8 } static int Main() { return 0; } }
In this case, Townhouse would have a value of 13 because
it follows SingleFamily that has a value of 12.
No comments:
Post a Comment