How do you pass multiple enum values in C#?

后端 未结 10 1886
灰色年华
灰色年华 2020-11-29 16:50

Sometimes when reading others\' C# code I see a method that will accept multiple enum values in a single parameter. I always thought it was kind of neat, but never looked in

相关标签:
10条回答
  • 2020-11-29 16:53
    [Flags]
        public enum DaysOfWeek{
    
    
            Sunday = 1 << 0,
            Monday = 1 << 1,
            Tuesday = 1 << 2,
            Wednesday = 1 << 3,
            Thursday = 1 << 4,
            Friday =  1 << 5,
            Saturday =  1 << 6
        }
    

    call the method in this format

    MethodName(DaysOfWeek.Tuesday | DaysOfWeek.Thursday);

    Implement a EnumToArray method to get the options passed

    private static void AddEntryToList(DaysOfWeek days, DaysOfWeek match, List<string> dayList, string entryText) {
                if ((days& match) != 0) {
                    dayList.Add(entryText);
                }
            }
    
            internal static string[] EnumToArray(DaysOfWeek days) {
                List<string> verbList = new List<string>();
    
                AddEntryToList(days, HttpVerbs.Sunday, dayList, "Sunday");
                AddEntryToList(days, HttpVerbs.Monday , dayList, "Monday ");
                ...
    
                return dayList.ToArray();
            }
    
    0 讨论(0)
  • 2020-11-29 16:54
    [Flags]
    public enum DaysOfWeek
    {
      Mon = 1,
      Tue = 2,
      Wed = 4,
      Thur = 8,
      Fri = 16,
      Sat = 32,
      Sun = 64
    }
    

    You have to specify the numbers, and increment them like this because it is storing the values in a bitwise fashion.

    Then just define your method to take this enum

    public void DoSomething(DaysOfWeek day)
    {
      ...
    }
    

    and to call it do something like

    DoSomething(DaysOfWeek.Mon | DaysOfWeek.Tue) // Both Monday and Tuesday
    

    To check if one of the enum values was included check them using bitwise operations like

    public void DoSomething(DaysOfWeek day)
    {
      if ((day & DaysOfWeek.Mon) == DaysOfWeek.Mon) // Does a bitwise and then compares it to Mondays enum value
      {
        // Monday was passed in
      }
    }
    
    0 讨论(0)
  • 2020-11-29 16:54

    Something of this nature should show what you are looking for:

    [Flags]
    public enum SomeName
    {
        Name1,
        Name2
    }
    
    public class SomeClass()
    {
        public void SomeMethod(SomeName enumInput)
        {
            ...
        }
    }
    
    0 讨论(0)
  • 2020-11-29 16:57

    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).

    0 讨论(0)
  • 2020-11-29 17:02

    I think the more elegant solution is to use HasFlag():

        [Flags]
        public enum DaysOfWeek
        {
            Sunday = 1,
            Monday = 2,
            Tuesday = 4,
            Wednesday = 8,
            Thursday = 16,
            Friday = 32,
            Saturday = 64
        }
    
        public void RunOnDays(DaysOfWeek days)
        {
            bool isTuesdaySet = days.HasFlag(DaysOfWeek.Tuesday);
    
            if (isTuesdaySet)
            {
                //...
            }
        }
    
        public void CallMethodWithTuesdayAndThursday()
        {
            RunOnDays(DaysOfWeek.Tuesday | DaysOfWeek.Thursday);
        }
    
    0 讨论(0)
  • 2020-11-29 17:02

    Mark your enum with the [Flags] attribute. Also ensure that all of your values are mutually exclusive (two values can't add up to equal another) like 1,2,4,8,16,32,64 in your case

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

    When you have a method that accepts a DayOfWeek enum use the bitwise or operator (|) to use multiple members together. For example:

    MyMethod(DayOfWeek.Sunday|DayOfWeek.Tuesday|DayOfWeek.Friday)
    

    To check if the parameter contains a specific member, use the bitwise and operator (&) with the member you are checking for.

    if(arg & DayOfWeek.Sunday == DayOfWeek.Sunday)
    Console.WriteLine("Contains Sunday");
    
    0 讨论(0)
提交回复
热议问题