【问题标题】:adding booleans (as integer) [duplicate]添加布尔值(作为整数)[重复]
【发布时间】:2012-09-01 06:39:37
【问题描述】:

我在 AS3 中编写了一个代码,它允许我检查特定数量的事情是否为真......

If (true + false + true + true + false + true + true < 4)
{

}

当我尝试用 C# 重写时,它告诉我不能添加类型 bool 和 bool。这样做的最好方法是像这样重写它吗?还是有一些更简单的解决方法?

If ((true?1:0) + (false?1:0) + (true?1:0) + (true?1:0) + (false?1:0) + (true?1:0) + (true?1:0) < 4)
{

}

【问题讨论】:

标签: c# boolean


【解决方案1】:

尝试在params方法参数上使用System.Linq中的IEnumerable&lt;T&gt;.Count(Func&lt;T,bool&gt;),将T作为bool

public static int CountTrue(params bool[] args)
{
   return args.Count(t => t);
}

用法

// The count will be 3
int count = CountTrue(false, true, false, true, true);

还可以引入this扩展方法

public static int TrueCount(this bool[] array)
{
   return array.Count(t => t);
}

用法

// The count will be 3
int count = new bool[] { false, true, false, true, true }.TrueCount();

【讨论】:

  • 不错的一个!非常干净优雅的代码!
  • 太漂亮了 :') 谢谢,这正是我要使用的!
  • +1 是的,使用params的好主意
【解决方案2】:

您可以为BitArray 类编写扩展方法。我不确定性能是否会比使用 Linq 更好,但至少它是另一种选择:

public static int CountSetBits(this BitArray theBitArray)
{
   int count = 0;
   for (int i = 0; i < theBitArray.Count; i++)
   {
      count += (theBitArray.Get(i)) ? 1 : 0;
   }
   return count; 
}

用法:

BitArray barray = new BitArray(new [] { true, true, false, true });
int count = barray.CountSetBits(); // Will return 3

【讨论】:

    【解决方案3】:

    (受 L.B.:s 评论启发)你可以写一个扩展方法:

    static class Ex
    {
        public static int Int(this bool x) { return x ? 1 : 0; }
    }
    

    然后(假设您在包含Ex 类的命名空间中包含using),您可以像这样编写if 语句:

    if (true.Int() + false.Int() + true.Int() + true.Int() + 
        false.Int() + true.Int() + true.Int() < 4)
    {
        ...
    }
    

    【讨论】:

      【解决方案4】:

      这是一个更有趣的例子:

      if ((BoolCount)true + false + true + true + false + true + true <= 5)
      {
          Console.WriteLine("yay");
      }
      

      使用这个类:

      struct BoolCount
      {
          private readonly int c;
          private BoolCount(int c) { this.c = c; }
      
          public static implicit operator BoolCount(bool b)
              { return new BoolCount(Convert.ToInt32(b)); }
      
          public static implicit operator int(BoolCount me)
              { return me.c; }
      
          public static BoolCount operator +(BoolCount me, BoolCount other)
              { return new BoolCount(me.c + other.c); }
      }
      

      【讨论】:

        【解决方案5】:

        Convert.ToInt32(true) + Convert.ToInt32(false) + Convert.ToInt32(true) 在这种情况下也可以工作,我认为这是我们拥有的最简单的方法

        【讨论】:

          【解决方案6】:

          你可以创建一个数组并使用Count:

          if ((new []{true, false, true, true, false, true, true}).Count(x=>x) < 4)
          {
          
          }
          

          Sum 方法:

          if ((new []{true, false, true, true, false, true, true}).Sum(x=>x?1:0) < 4)
          {
          
          }
          

          【讨论】:

          • 如果我是一次性使用它,我会这样做,谢谢大家回复upvotes
          • 谢谢!这是我最喜欢的方法。我需要像这样添加一些布尔值,并且 (new[] {var1, var2, var3, var4}).Sum(x => x ? 1 : 0);对我来说效果很好!
          猜你喜欢
          • 2014-09-03
          • 2021-03-24
          • 2021-03-06
          • 1970-01-01
          • 2011-12-10
          • 1970-01-01
          • 2016-05-01
          • 1970-01-01
          相关资源
          最近更新 更多