Do You Know These Basic Concepts ?

Enum Basics:-

In C# Language Enum are the type.

Enum are defined by following way :-


enum Month {Jan,Feb,March,April}
//To Access the Enum we do by the following way :-
Console.WriteLine("The Value of Enum is {0}",Month.Jan);
///OutPut You got as : The Value of Enum is Jan

 Do You Know :-

  • Even though you assigning the names to the enum member list; the compiler actually assigns integer value to the member of the list starting with Zero(0) and incrementing  by each of successive members.
  • If you want to know about the assign integer value of particular enum member in the list then you need type cast it to integer by using Convert.Toint32() method.

    For eg :- Finding integer value :-
    Console.WriteLine(Convert.toInt32(Month.Jan));
    Output:- 0;

  • You can also give the custom enum list value programmatically.

    For eg :-
    enum Month {Jan=2,Feb=3,March=5};
    Console.writeline(Convert.toInt32(Month.Jan));
    Output:-2.

  • By default compiler initializing int value to the list of Enum starting form Zero(0).
    We can make change the data type of Enum as well through programmatically

    Changing Default DataType of Enum form int to byte type:-
    enum Month:byte {Jan=2,Feb=3,March=5};
    Console.writeline(sizeof(Month));
    Output:-1. ///Try it by replacing byte to int you get O/P:-4

Hope it’s helpful to You

Keep Learning and Coding 🙂

Any feedback reply me !!!

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s