enum in C# and conversion

Cast int to enum in C# c# - convert an enum string How do I Convert a string to an enum in C#? C# Enum ToString Method C# Enum Examples Associating Strings with enums in C#

An enumeration type (also known an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable to make programming clear, understandable and manageable. There are ways of using, like how to use enum in switch and case statement, how to convert a text to enum or number to enum to compare, we will discuss here all these points in detail.

Let’s first understand the basics of enum in C#

  • The body of an enum type declaration defines zero or more enum members, which are the named constants of the enum type.
  • No two enum members can have the same name.
  • Each enum member has an associated constant value.

  • By default the underlying type of each element in the enum is int.

  • All the underlying elements by default are public
  • You can verify the underlying numeric values by casting to the underlying type (we will see it in this article latter).

How to create enum

public enum Days 
{ 
   Sunday, 
   Monday, 
   Tuesday, 
   Wednesday, 
   Thursday, 
   Friday, 
   Saturday 
}

In this case all the underlying elements type is integer so how to change elements type to something different than integer, can we do this, yes, to do this we can provide other integral type after colon like we inherit a class from other class, see this

public enum Months : byte 
{ 
  Jan, Feb, Mar,  Apr, May, Jun,  Jul, Aug, Sep,  Oct, Nov, Dec 
}

In this case all the underlying elements type is byte. In both the above case we have not defined any integer value to any element but it will contain from 1 to n, it doesn’t start from 0 as in the case of array but from 1. I there any way to define an enum with my own values, yes, let’s see this, suppose we want to create an enum category which start from 100 and 200 and so on then

public enum CarCategory
{
Convertible = 100,
Hatchback = 200,
Sedan = 300,
SUV = 400,
VAN = 500,
Truck = 600,
Wagon = 1000
}

As you can see here we used 1000 for Wagon after Truck =600, so sequential increment is not needed, other thing we need to note here is value assigned to any member can not exceed the range of underlying type, let's see this

enum xyz : uint
{
  x   =-1,
  y = -2,
  z = -3
}

It is wrong because -1, -2, -3 etc. out of range of uint.

Have you ever tried to use enum in switch case statement, let's see how we can do this, we will use CarCategory for this example, suppose we are getting integer value from our code and we have to use that integer in switch and execute matching category then

Int32[]arr = {100, 300, 1000};
foreach (Int32 category in arr)
{
   switch ((CarCategory)category)
   {
        case CarCategory.Convertible:
           // Do something
           break;
        case CarCategory.Hatchback:
           // Do something
           break;
        .........................
        .........................            
        default: break;
   }
}

We convert our integer value to CarCategory enum in siwtch, if any item match it will execute matching case if not then execute default case. Suppose we are getting a value 2000 which is not defined in our enum, what will happen then, will it give error, no it will execute default case, let try to convert some number into car category enum

(CarCategory)100 => Convertible 
(CarCategory)500 => VAN
(CarCategory)1000 => Wagon 
(CarCategory)2000 =>  2000 and type would be CarCategory

How to get enum member's integer and string value?

CarCategory.Wagon.ToString(); 
// result : Wagon 
Convert.ToInt32(CarCategory.Wagon);
// result : 1000
// same we can use like this
(String)CarCategory.Wagon;
(int)CarCategory.Wagon;
Hamden Process manager with a reputed organization, Fond of learning new features and technology related to C#, ASP.Net, SQL Server, MVC etc.I like to help others, if I can
  • c#
  • oops
By Hamden On 27 Mar, 13  Viewed: 4,525

Other blogs you may like

Polymorphism in C# with example

Polymorphism is derived from two Latin words, 1. Poly means many 2. Marphose means forms. By using inheritance, a class can be used as many types, say we have a Person which may be a Professor, may be a Student, may be Clerk. So in this article we will see some example of Polymorphism. When we... By Hamden   On 01 Jun 2013  Viewed: 4,548

How to create windows service in C# visual studio 2010

Creating windows service in C# is really easy but when we need to create first time we face many problems so in this post we will try to learn everything about window service. In this article we will create a windows service which will run after a particular interval, and time should be... By Ali Adravi   On 30 Apr 2013  Viewed: 8,610

Covariance and Contravariance in C# 4.0 with example

C# the language, right from the very beginning is always supported covariance and contravariance but in most simple scenario, now in C# 4.0 we have full support of covariance and contravariance in all circumstances, generic interface and generic delegates, other C# types already support from first... By Ali Adravi   On 29 Mar 2013  Viewed: 4,977

default value for different data type in C#

In C# there are different data type and they use some default value when we declare a variable. When we define a variable of type int or Int32 say int score; so what is the value of score, will it be null or zero, in the same way if we create a variable of type string/String what value it holds... By Ali Adravi   On 27 Mar 2013  Viewed: 2,599

Dynamic vs var in C# with example

When we have var which dynamically holds any type or value so why we need dynamic type, what are the differences and situations where we can use dynamic rather than var, there were some question in my mind so I explored more which I want to share here. There are two things, static type and... By Nathan Armour   On 26 Mar 2013  Viewed: 3,204