Aaron
Aaron

Reputation: 11693

What would I use an Enum for?

I've declared an enum type, assigned a variable to it and now I am writing it to the console. So what use does an enum type have in a real world application?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
   class Enum
{
    enum cars
    {
        Toyota, Nissan, Ferrari, Lamborghini
    }
}
class Program
{
    enum cars
    {
        Toyota, Nissan, Ferrari, Lamborghini
    }
    static void Main(string[] args)
    {
        int a = (int)cars.Ferrari;
        Console.WriteLine(a);
    }
}
}

Upvotes: 3

Views: 1823

Answers (10)

Damith
Damith

Reputation: 63105

Whenever a procedure accepts a limited set of variables, consider using an enumeration. Enumerations make for clearer and more readable code, particularly when meaningful names are used.

The benefits of using enumerations include:

  • Reduces errors caused by transposing or mistyping numbers.

  • Makes it easy to change values in the future.

  • Makes code easier to read, which means it is less likely that errors will creep into it.

  • Ensures forward compatibility. With enumerations, your code is less likely to fail if in the future someone changes the values corresponding to the member names.

ref : MSDN

Upvotes: 10

kurupt_89
kurupt_89

Reputation: 1592

Heres an example of using enums in asp.net to change the header texts of each column. I Used the enum to specify the index of which column in the gridview I want to alter.

private enum MENU_ITEM
{ 
    Customer_ID = 0,
    Customer_First_Name = 1,
    Customer_Surname = 2,
    Customer_DOB = 3,
    Customer_Phone_Number = 4,
    Customer_Email = 5,
    Customer_Update = 6,
    Customer_Delete = 7,
    Customer_Transaction = 8
}

private void populateGridHeader()
{
    SearchCustomer_g.Columns[(int)MENU_ITEM.Customer_ID].Visible = false;
    SearchCustomer_g.Columns[(int)MENU_ITEM.Customer_First_Name].HeaderText = "First Name";
    SearchCustomer_g.Columns[(int)MENU_ITEM.Customer_Surname].HeaderText = "Surname";
    SearchCustomer_g.Columns[(int)MENU_ITEM.Customer_DOB].HeaderText = "Date of Birth";
    SearchCustomer_g.Columns[(int)MENU_ITEM.Customer_Phone_Number].HeaderText = "Phone Number";
    SearchCustomer_g.Columns[(int)MENU_ITEM.Customer_Email].HeaderText = "Email";
    SearchCustomer_g.Columns[(int)MENU_ITEM.Customer_Transaction].HeaderText = "New Transaction";
}

Upvotes: 0

Connell
Connell

Reputation: 14421

One thing no-one has pointed out yet (unless I missed it) is the use of Enumeration Types to set bit flags. You can do bitwise operations (such as & and |) on enum values and also bitwise comparisons. This works with the FlagsAttribute attribute.

[Flags]
enum Days
{
    None = 0x0,
    Sunday = 0x1,
    Monday = 0x2,
    Tuesday = 0x4,
    Wednesday = 0x8,
    Thursday = 0x10,
    Friday = 0x20,
    Saturday = 0x40
}

class MyClass
{
    Days meetingDays = Days.Tuesday | Days.Thursday;
}

Upvotes: 2

You want to use enum values in your program to improve code clarity and make it easier to maintain. Enums provide better error-checking and compiler warnings. They store constants and important values. Check the Enums for a better clarification.

EDIT:

  • Enums can be used with IntelliSense in Visual Studio in real world application.
  • They come in handy when you wish to be able to choose between a set of constant values, and with each possible value relating to a number, they can be used in a wide range of situations.
  • An enumeration type provides an efficient way to define a set of named integral constants that may be assigned to a variable.

Upvotes: 2

Sandeep Pathak
Sandeep Pathak

Reputation: 10747

The following are advantages of using an enum :

  1. You clearly specify for client code which values are valid for the variable.

  2. In Visual Studio, IntelliSense lists the defined values.

Upvotes: 0

Robel Sharma
Robel Sharma

Reputation: 972

Enum class contains many useful methods for working with enumerations. The beauty of enum is that your can process it as integer value and display as string.

You can find more in >> http://www.c-sharpcorner.com/uploadfile/puranindia/enums-in-C-Sharp/

Upvotes: 1

Merlyn Morgan-Graham
Merlyn Morgan-Graham

Reputation: 59151

enum cars
{
    Toyota, Nissan, Ferrari, Lamborghini
}

This isn't the best example, as there are way more types of car than that, and new car manufacturers pop up regularly, e.g. tiny custom shops.

What would I use an Enum for?

You'd use it for something that has more than once choice, but those choices are discrete, and aren't going to change (very often).

You'd use it in places that might otherwise require a string, but where you don't want to accept just any string.

Something like:

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

public void ScheduleRecurringAppointment(DayOfWeek day)
{
    // Todo: Add appointment to DB here...
}

(note that this isn't an enum you should write yourself. There is one in the .Net framework already).

You can change enums, but it is painful, as you have to recompile all code that uses the enum.

Upvotes: 2

StuartLC
StuartLC

Reputation: 107357

Enums are for code readability, and can be used to restrict a type to a finite set of values.

A common use of Enums is for modelling of States in a business process - i.e. New States cannot be added on an adhoc basis, and would require further coding and testing.

Upvotes: 0

Fischermaen
Fischermaen

Reputation: 12468

You can variables of your enum type. They will only accept the values of the enum and that makes coding easier (I think)

For example:

Enum Cars { VW, Mercedes, Ford, Opel };

...

Cars myCar = Cars.VW;

Upvotes: 0

larsmoa
larsmoa

Reputation: 12942

This would probably be more useful:

public enum Transmission {
  Manual,
  Automatic,
  SemiAutomatic
}

public class Car
{
   public Transmission CarTransmission { get; set; }
}

Upvotes: 1

Related Questions