Do You Know These Basic Concepts Of C#

Enum Basics

In C# language, enum is the data type.

Enum is defined according to-
  1. enum Month {Jan,Feb,March,April}     
  2. //To Access the Enum we do by the following way :-    
  3.  Console.WriteLine("The Value of Enum is {0}",Month.Jan);    
  4.  ///OutPut You got as : The Value of Enum is Jan        
Do You Know
  • Even though, you are assigning the names to the enum member list; the compiler actually assigns an integer value to the member of the list starting with zero(0) and increment each for the successive members.

  • If you want to know about assigning integer value of particular enum member in the list, you need to type cast it to the integer, using Convert.Toint32() method.

    For eg :- Finding integer value.
    1. Console.WriteLine(Convert.toInt32(Month.Jan));  
    Output:  0
     
  • You can also give the custom enum list value programmatically.
     
    For Example
    1. enum Month {Jan=2,Feb=3,March=5};  
    2. Console.writeline(Convert.toInt32(Month.Jan));  
    Output: 2.
     
  • By default, the compiler initializes int value to the list of Enum starting form zero(0). We can make change in the data type of enum as well through programmatically, changing default data type of enum form int to byte type.
    1. enum Month:byte {Jan=2,Feb=3,March=5};  
    2. Console.writeline(sizeof(Month));  
    Output: 1 ///Try it by replacing byte to int and you get output as 4.
Next Recommended Reading C# Concepts With Real-World Examples