Objected Oriented Programming and C#: Enumerations

Enumerations are lists of named integer constants. They are defined in a similar manner to classes, but instead function as lists that assign an integer a name that can be used to reference it.

enum

When the following code is implemented, the console will display “Friday” then “4” on the next line. The first instruction tells the console to display the string placeholder for the integer and the second line displays the actual integer value from the list. Even if no direct assignments are made, the members of the enumeration have default integers assigned to them. By default, the first integer is 0 and this increments by one for every member of the list. The members of the list can be used with methods to convert to other types such as to a string, as shown.

convert

The built in method “ToString()” is used to create a string based on the member “Monday” of the enumeration “WeekDay”.

The main purpose of enumerations are to increase code readability by giving constants meaningful names. The first constant value can be set to a specific value. If no other assignments are made, the list will automatically increment.

read

The code above will display “10” and “14” for example.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s