Enums In C#

Posted on Updated on

Enums In C#

Before grabbing hand over the Enumerated Data types, let us discuss about two types of data types available in programming :

 Primitive ( Predefined data types)

 User Defined Data types.

Primitive Data Types: Now in this case I am not gonna repeat those traditional definitions as are proclaimed by different encyclopedias. Primitive data types are the one can say those data types whose size is pre – defined like integer only accept those numbers which doesn’t contain any points in it and of 2 bytes in traditional languages like C,C++ etc.

What if we want to store the information of the book? Like its color, number of pages and price. Now we can’t store all values into a single variable. As color is of string type, number of pages is of int type and price is of float type.Alternatively one can store all the values by creating a user defined data type like classes, structures, unions etc.

So User Defined Data Types are those data types that are being defined by the user rather than by the compiler itself.

Image result for Enums In C#

Enum is also one of them that is used to defined named integer collection. Like we want to give names some integer values.

Syntax:

enum <enum_name>

{

collection of enums

};

Example:

enum color {red,blue,white};

In enums, the first element of array by default gets 0 value, and next element gets higher value as compare to preceding element. It is easy to remember that there are 101 teams of football rather than remembering,  each team by its name. If you want to change the order of enums like you want to start enum from 2 number you can simply modify the number as stated below:

enum color{red, blue=2,yellow}

In aforesaid example, color enum will be created and as we given 3 value to blue element thus its next element by default achieves 3 value and so on.

How to access element of an enum??

We can simply access the element of enum by calling it using enum’s name.

Syntax:

console.WriteLine(“Color number of yellow is {0}”,color.yellow)

or

int code= (int)color.yellow

Here, we have implicitly casting the variable of enum type to integer value this type of casting named as Explicit Casting.Thereafter we can simply print the value using console. Write or

WriteLine function

console.writeline(“Color code for yellow is {0}”,code)

Output:

Color code for yellow is: 3

It is as similar as that you assign roll numbers to students different names and in conclusion it provides a unique identity to the elements. And after it simply you can access those elements using these unique identifiers.

Program written in C#

using System;

/*Enum – Enum is the set of name inetger constants. It is declared using enum keyword.

 * Enum contains its own values and cannot be inherited.

 * Syntax enum <enum_name> { list };

 * Every element inside enum got an integer value one greater than the preceeding value.

 * By default, the value of the first element is 0.

 * enum Days{Sun, Mon, Tue, Wed, Thu, Fri, Sat};

 * */

namespace Enums

{

    class Program

    {

        enum Days{ Sun, Mon, Tue, Wed, Thus, Fri, Sat };

        static void Main(string[] args)

        {

          int startingday=  (int) Days.Mon;

          int endday = (int)Days.Fri;

          Console.WriteLine(“Monday : {0} “, startingday);

          Console.WriteLine(“Saturday : {0} “, endday);

          Console.ReadKey();

        }

    }

}

*****************************************************************************************************

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.
Helpline: 9814666333, 8699444666
Email: info@technocampus.co.in

Please fill the form and we shall contact you soon.

Advertisements

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