by Edward
12 August 2010 08:49
If you are working with Enumerations (enum) in C#, you will know how easy it is to use within your code. Enum's are strongly typed constants which are unique types and allows you to assign symbolic names to integral values.
Enum is also a reserved keyword and is a value type, which means we can use it as a string, if the need arises. I have added an example where I would like to write out the Gender value of a enum. Because I can't just write the value to screen, I need to convert it to a string, which is actually very easy.
The following example demonstrates converting an enumerated value to a string.
public class GenderSample {
enum Gender {Male = 1, Female = 2};
public static void Main() {
Enum GenderOptions = Gender.Male;
Console.WriteLine("The value of this instance is '{0}'",
GenderOptions.ToString());
}
}
Output: The value of this instance is 'Male'.