计算 Enum 参数的数量

我是 C# 中枚举的新手。我将多个枚举值作为参数传递给方法。在方法内部我想知道我有什么价值。


让我解释。


Class A

{

        [Flags]

        public enum days

        {

            sunday =1,

            monday =2,

            tuesday= 4

        }

static void Main(string[] args)

        {

            dummy(days.sunday|days.monday);

            dummy(days.sunday);


        }


public static void dummy(days Daysofweek)

        {

            if(\* Daysofweek is oly sunday*/){/*some function*/}

            else if(\* Daysofweek is sunday and monday*/){/*some other  function*/}

            Console.WriteLine(Daysofweek.ToString());


        }

}

'''


我想要实现的只是“如果参数中只有一个值,则做某事,否则,如果有多个值,则做其他事情”。


手掌心
浏览 133回答 5
5回答

胡说叔叔

我想要实现的只是“如果参数中只有一个值,则做某事,否则,如果有多个值,则做其他事情”。由于Flags值中是单个位,因此是 2 的幂,因此您可以使用uint v; // we want to see if v is a power of 2bool f; // the result goes here f = (v & (v - 1)) == 0;检查该值是否为 2 的幂,如果不是,则设置了多个标志。但请记住这一点请注意,此处 0 被错误地视为 2 的幂。public static void dummy(days Daysofweek){    int val = (int) Daysofweek;    bool hasMultipleFlagsSet = val != 0 && (val & (val - 1)) == 0;    if(hasMultipleFlagsSet){/*some function*/}    else {/*some other  function*/}    Console.WriteLine(Daysofweek.ToString());}

阿波罗的战车

这是一个通用的解决方案:public static int CountFlags( Enum days ){    int flagsSet = 0;    foreach( var value in Enum.GetValues( days.GetType() ) )    {        if( days.HasFlag( (Enum)value ) )        {            ++flagsSet;        }    }    return flagsSet;}

LEATH

使用HasFlagusing System;[Flags] public enum Pets {   None = 0,   Dog = 1,   Cat = 2,   Bird = 4,   Rabbit = 8,   Other = 16}public class Example{   public static void Main()   {      Pets[] petsInFamilies = { Pets.None, Pets.Dog | Pets.Cat, Pets.Dog };      int familiesWithoutPets = 0;      int familiesWithDog = 0;      foreach (var petsInFamily in petsInFamilies)      {         // Count families that have no pets.         if (petsInFamily.Equals(Pets.None))            familiesWithoutPets++;         // Of families with pets, count families that have a dog.         else if (petsInFamily.HasFlag(Pets.Dog))            familiesWithDog++;      }      Console.WriteLine("{0} of {1} families in the sample have no pets.",                         familiesWithoutPets, petsInFamilies.Length);         Console.WriteLine("{0} of {1} families in the sample have a dog.",                         familiesWithDog, petsInFamilies.Length);      }}// The example displays the following output://       1 of 3 families in the sample have no pets.//       2 of 3 families in the sample have a dog.

波斯汪

转换为整数,然后计算设置为 1 的位数:&nbsp; &nbsp; &nbsp; &nbsp; public enum DAYS&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sunday =1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; monday =2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tuesday= 4&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DAYS days =&nbsp; DAYS.sunday | DAYS.monday;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int numDays = CountDays(days);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; static int CountDays(DAYS days)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int number = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < 32; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; number += ((uint)days & (1 << i)) == 0 ? 0 : 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return number;&nbsp; &nbsp; &nbsp; &nbsp; }

蝴蝶不菲

我想要实现的只是“如果参数中只有一个值,则做某事,否则,如果有多个值,则做其他事情”。假设您没有向枚举添加任何“组合”命名值(例如,您没有Weekend预先设置为 的成员Sunday|Saturday),您可以使用Enum.GetName对于枚举的命名成员以及null多个成员的任意组合,它将返回一个非空字符串。
打开App,查看更多内容
随时随地看视频慕课网APP