Matching an integer to an enum value

With an enum data type, you can specify just string values, or you can also specify an integer value along with the string.

e.g.

Enum with string values
     public enum
Enum with string and integer values
public enum EnumWithStringAndInt
{
   ItemOne=1,
   ItemTwo=2
}

One great way to make use of this is when using the enum as a method parameter.
This is cool because the input can be either be the integer, or the string value.

An example could be a payment method enum

public static enum PaymentMethods
{
   CreditCard=1,
   Invoice=2,
   DirectDebit=3
}

So, if someone wanted to make a payment, they could call the following example method

public void MakePayment(decimal price, enum paymentMethods)
{
   ....
}

public void Implementation()
{
   MakePayment(10.00, PaymentMethods.CreditCard)
}

This was fantastic for a public-facing API, because it gave the client some flexibility.

Subscribe

0 comments