tags:

views:

135

answers:

4

What is main use of Enumeration in c#?

Edited:- suppose I want to compare the string variable with the any enumeration item then how i can do this in c# ?

+3  A: 

An enumeration type (also named an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable.

http://msdn.microsoft.com/en-us/library/cc138362.aspx

anishmarokey
source: http://msdn.microsoft.com/en-us/library/cc138362.aspx
Dror
+7  A: 

The definition in MSDN is a good place to start.

An enumeration type (also named an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable.

The main benefit of this is that constants can be referred to in a consistent, expressive and type safe way.

Take for example this very simple Employee class with a constructor:

You could do it like this:

public class Employee
{
    private string _sex;

    public Employee(string sex)
    {
       _sex = sex;
    }
}

But now you are relying upon users to enter just the right value for that string.

Using enums, you can instead have:

public enum Sex
{
    Male = 10,
    Female = 20
}

public class Employee
{
    private Sex _sex;

    public Employee(Sex sex)
    {
       _sex = sex;
    }
}    

This suddenly allows consumers of the Employee class to use it much more easily:

Employee employee = new Employeer("Male");

Becomes:

Employee employee = new Employeer(Sex.Male);
David Hall
+2  A: 

There are two meanings of enumeration in C#.

An enumeration (noun) is a set of named values. Example:

public enum Result {
  True,
  False,
  FileNotFound
}

Enumeration (noun form of the verb enumerate) is when you step through the items in a collection.

The IEnumerable<T> interface is used by classes that provide the ability to be enumerated. An array is a simple example of such a class. Another example is how LINQ uses it to return results as enumerable collections.

Edit:

If you want to compare a string to an enum value, you either have to parse the string to the enum type:

if ((Result)Enum.Parse(typeof(Result), theString) == Result.True) ...

or convert the enum value to a string:

if (theString == Result.True.ToString()) ...

(Be careful how you compare the values, depending on whether you want a case sensetive match or not.)

If you want to enumerate a collection and look for a string, you can use the foreach command:

foreach (string s in theCollection) {
  if (s == theString) {
    // found
  }
}
Guffa
if ((Result)Enum.Parse(Result, theString) == Result.True) Correction if ((Result)Enum.Parse(typeof(Result), theString) == Result.True) as per my knowledge
Lalit
Anyways, which will be best perform wise ? first method (parsing method) of comparison. or second method
Lalit
@Lalit: Thanks, I made the correction. I think that the performance will be pretty much the same. It's probably somewhat faster to turn an enum into a string, but on the other hand it's slower to compare strings than enum values.
Guffa
+1  A: 

Often you find you have something - data, a classification, whatever - which is best expressed as one of several discrete states which can be represented with integers. The classic example is months of the year. We would like the months of the year to be representable as both strings ("August 19, 2010") and as numbers ("8/19/2010"). Enum provides a concise way to assign names to a bunch of integers, so we can use simple loops through integers to move through months.

Spencer Nelson