Saturday, February 16, 2013

Enumerators Description

Enumerations

C# allows constants and enumerations to be declared. Constants allow names to be assigned to fixed values of varying types. An enumeration defines a number of related constants for numeric values, each with a unique name. This can enhance the readability of code, as the names of constants can be self-documenting. 

The Description attribute can be applied to each of the constants in an enumeration. It is found in the System.ComponentModel namespace so add the following using directive to simplify the code.

using System.ComponentModel;
Let us test that the description works with enum items.

enum CarColour
{
    [Description("Ice Cream White")]
    White,
    [Description("Carbon Black")]
    Black,
    [Description("Sterling Silver")]
    Silver,
}

Testing the Extension Method 

Console.WriteLine(CarColour.White.Description());

/* OUTPUT
Ice Cream White
*/

 

No comments:

Post a Comment