English 中文(简体)
如果声明 - 或声明 - 但是不是和
原标题:If statement - or but NOT and

在C Sharp 中,我怎么能建立这样的声明呢? 如果一个条件是真实的呢? 它必须只是其中的一个条件,如果一个或两个以上条件是真实的,如果是虚假的,那我怎么能建立这样的声明呢?

最佳回答

你可以写一个帮手方法。这个方法的优点是短路, 只能精确地评估所需的数量,

public static bool IsExactlyOneTrue(IEnumerable<Func<bool>> conditions) {
    bool any = false;
    foreach (var condition in conditions) {
        bool result = condition();
        if (any && result) {
            return false;
        }
        any = any | result;
    }
    return any;
}
问题回答
List<Func<Customer, bool>> criteria = new List<Func<Customer, bool>>();

criteria.Add(c => c.Name.StartsWith("B"));
criteria.Add(c => c.Job == Jobs.Plumber);
criteria.Add(c => c.IsExcellent);

Customer myCustomer = GetCustomer();

int criteriaCount = criteria
  .Where(q => q(myCustomer))
  // .Take(2)  // optimization
  .Count()
if (criteriaCount == 1)
{
}

执行杰森方法签字的林克:

public static bool IsExactlyOneTrue(IEnumerable<Func<bool>> conditions)
{
  int passingConditions = conditions
    .Where(x => x())
    // .Take(2) //optimization
    .Count();
  return passingConditions == 1;
}

您可以将您的布尔合成为 bool 序列, 然后应用 LINQ :

bool[] conditions = new bool[] { cond1, cond2, cond3, cond4 };
bool singleTrue = conditions.Count(cond => cond) == 1;

仅针对两个排他性或简单得多的布尔:

bool singleTrue = cond1 != cond2;

Edit :为了实现点火评价 短路路,我们需要将我们的 bool 序列推广为 Func<bool> 序列(其中每个元素是包含条件评价的函数代表):

IEnumerable<Func<bool>> conditions = // define sequence here
int firstTrue = conditions.IndexOf(cond => cond());
bool singleTrue = firstTrue != -1 && 
                  conditions.Skip(firstTrue + 1).All(cond => !cond());

上述片段假定存在一个以上游为基础的 Indexof 操作员,根据目前版本的LINQ, 无法使用该操作员,但可被界定为类似的扩展方法:

public static int IndexOf<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
    int i = 0;

    foreach (T element in source)
    {
        if (predicate(element))
            return i;

        i++;
    }

    return -1;
}

用于测试的样本数据(可在每个 false ret 上设定一个断点,以跟踪评估):

IEnumerable<Func<bool>> conditions = new Func<bool>[] 
{ 
    () => 
        false,
    () => 
        true,
    () => 
        false,
    () => 
        false,
};

为了简单起见,你可以保持一个计数:

int totalTrue = 0;
if (A) totalTrue++;
if (B) totalTrue++;
if (C) totalTrue++;
...
return (1 == totalTrue);

我觉得这样能解决问题

 int i= 0;
 if ( (!A || ++i <= 1) && 
      (!B || ++i <= 1) && 
      (!C || ++i <= 1) && 
      ... && 
      (i == 1))

如果我没有错觉, 只要 i> 1 , 这个 if 就会是假的。 如果 i> 1 从未加载, 我们到达最后的孔, 那么由于 i = 0 将是假的 。

但最简单的答案是:

if( (A & !(B || C)) || 
    (B & !(A || C)) ||
    (C & !(A || B)) )
{
   ...
}

最后,你对 A/B/C 的评审超过一次,因此,只有在有简单的布尔时才真正有用。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签