Here is how to declare an Enumeration type called team whose literal values are the name of soccer clubs.
enum team {Barcelona, Marseille, Arsenal, Galaxy}
Note: Internally, an enumeration associates an integer value with each element. By default, the numbering start at 0for the first element and goes up
{Barcelona(0), Marseille(1), Arsenal(2), Galaxy(1)}
Using an Enumeration:
enum team {Barcelona, Marseille, Arsenal, Galaxy}
class Soccer
{
public void Method(team parameter)
{
team localVariable;
}
private team currentTeam;
}
Note: Before you can use the value of an enumeration variable, it must be assigned a value. You can only assign valid values to an enumeration. For Example
team french = team.Marseille;
Console.writeLine(french) //writes out 'Marseille'
Note: You have to write team.french rather than french
If needed you can explicictly convert an enumeration variable to a string that represents its current value.
string country = french.toString();
console.WriteLine(country); //also writes out 'Marseille'
To retreive the Underlying Integer value of an Enumeration:
enum team {Fconnection, Marseille, Arsenal, Galaxy}
team french = team.Marseille;
Console.WriteLine((int)french) //writes out '1'
Choosing Enumeration Literal Value:
enum team {Barcelona=1, Marseille=2, Arsenal=3, Galaxy=4}
Choosing Enumeration's Underlying Type:
enum team: short {Barcelona, Marseille, Arsenal, Galaxy}
note: The main reason of doing this is to save memory an int occupies more memory than a short
int range of value = -2147483648 and 2147483647
short range of value = -32768 and 32767
Visit:
Makai Studio
Currently rated 5.0 by 1 people
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5