How to Pass Multiple Enum Values in C#

How do you pass multiple enum values in C#?

When you define the enum, just attribute it with [Flags], set values to powers of two, and it will work this way.

Nothing else changes, other than passing multiple values into a function.

For example:

[Flags]
enum DaysOfWeek
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64
}

public void RunOnDays(DaysOfWeek days)
{
bool isTuesdaySet = (days & DaysOfWeek.Tuesday) == DaysOfWeek.Tuesday;

if (isTuesdaySet)
//...
// Do your work here..
}

public void CallMethodWithTuesdayAndThursday()
{
this.RunOnDays(DaysOfWeek.Tuesday | DaysOfWeek.Thursday);
}

For more details, see MSDN's documentation on Enumeration Types.


Edit in response to additions to question.

You won't be able to use that enum as is, unless you wanted to do something like pass it as an array/collection/params array. That would let you pass multiple values. The flags syntax requires the Enum to be specified as flags (or to bastardize the language in a way that's its not designed).

Pass multiple enum to methods and get their values

Here are few steps to follow to get flagged enum :

  1. Use 2 exp (n) integer (1, 2, 4, 8, 16, 32, ...)
    to define your enum. Why ? : Actually each active state of your
    enum will take a single bit of a 32 bits integer.
  2. Add the Flags attribute.

Then,

    [Flags] 
public enum Status
{
S1 = 1,
S2 = 2,
S3 = 4,
S4 = 8
}

You can use Enum.HasFlag to check if a specific status is active :

public void DoWork(Status s) 
{
var statusResult = Enum.GetValues(typeof(Status)).Where(v => s.HasFlag(v)).ToArray() ;

// StatusResult should now contains {1, 2}
}

C# Difference betwen passing multiple enum values with pipe and ampersand

When you do |, you select both. When you do &, you only what overlaps.

Please note that these operators only make sense when you apply the [Flags] attribute to your enum. See http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx for a complete explanation on this attribute.

As an example, the following enum:

[Flags]
public enum TestEnum
{
Value1 = 1,
Value2 = 2,
Value1And2 = Value1 | Value2
}

And a few test cases:

var testValue = TestEnum.Value1;

Here we test that testValue overlaps with Value1And2 (i.e. is part of):

if ((testValue & TestEnum.Value1And2) != 0)
Console.WriteLine("testValue is part of Value1And2");

Here we test whether testValue is exactly equal to Value1And2. This is of course not true:

if (testValue == TestEnum.Value1And2)
Console.WriteLine("testValue is equal to Value1And2"); // Will not display!

Here we test whether the combination of testValue and Value2 is exactly equal to Value1And2:

if ((testValue | TestEnum.Value2) == TestEnum.Value1And2)
Console.WriteLine("testValue | Value2 is equal to Value1And2");

Assign multiple values to enum elements

Ah I just used a function instead of directly typecasting. Far more easier than to implement something entirely different. I already have lot of code running on this so cant change it much but here is what I did.

public Country GetCountryByTaxID(int taxID)
{
if (taxID == 3 || taxID == 4)
{
return Country.USA;
}
else
{
return Country.Canada;
}
}

how to add multiple enum values with pipe based on conditions?

If SharedAccessBlobPermissions has been declared with [Flags] attribute
you can do some set arithemtics. If initially

  SharedAccessBlobPermissions t = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Read;

Addtion:

  // Add SharedAccessBlobPermissions.Delete and SharedAccessBlobPermissions.Clear 
t |= SharedAccessBlobPermissions.Delete | SharedAccessBlobPermissions.Clear;

Subtraction:

  // Remove SharedAccessBlobPermissions.Delete
t = (t | SharedAccessBlobPermissions.Delete) ^ SharedAccessBlobPermissions.Delete;

Combining Enum Values with Bit-Flags

Enum.HasFlag is what you want to use

Console.WriteLine("Custodian is in All: {0}", Role.All.HasFlag(Role.Custodian));

Just noticed that your enum should be defined like this with the Flags attribute and values spaced out by powers of 2

[Flags]
public enum Role
{
NormalUser = 1,
Custodian = 2,
Finance = 4,
SuperUser = Custodian | Finance,
All = Custodian | Finance | NormalUser
}

The reason powers of 2 are used for flagged enums is that each power of 2 represents a unique bit being set in the binary representation:

NormalUser = 1 = 00000001
Custodian = 2 = 00000010
Finance = 4 = 00000100
Other = 8 = 00001000

Because each item in the enum has a unique bit set this allows them to be combined by setting their respective bits.

SuperUser  = 6 = 00000110 = Custodian + Finance
All = 7 = 00000111 = NormalUser + Custodian + Finance
NormOther = 9 = 00001001 = NormalUser + Other

Notice how each 1 in the binary form lines up with the bit set for the flag in the section above.



Related Topics



Leave a reply



Submit