用于检查多个值是否相等的语法糖

syntactic sugar for checking equality with multiple value

我想 compare 2 equations,让我们说 2*2 and 2+2 一个单一的答案,4,使用 Visual Studio 2015,例如

if (4 == (2*2 && 2+2)) {...}

但它 returns 一个错误,"Operator '&&' cannot be applied to operands of type 'int' and 'int'"。我能想到的编写代码的唯一其他方法是:

if (4 == 2*2 && 4 == 2+2) {...}

这会起作用,但是当要比较很多值时会变得非常重复。有没有更简单的方法来实现这个?

var results = new[]{ 2 + 2, 2 * 2, ... };
if (results.All(r => r == 4)) {
    ...
}

这会收集集合 results 中所有操作的结果,并使用扩展方法 All 来验证指定的谓词是否适用于所有值;允许只写一次谓词。

只是抛出一个随机的想法

if (new Int32[] { 2 * 2, 2 + 2, ... }.All(t => t == 4))
{
    MessageBox.Show("Yup");
}
else
{
    MessageBox.Show("Nope");
}

对不起,对不起,我看错了我删除了我以前的代码。这执行了 OP 的逻辑。

All 是我能找到的唯一方法。

你可以写一个函数:

public static bool AllEqual<T>(T expectedResult, params T[] calculations)
    where T : IEquatable<T>
{
    return calculations.All(x => x.Equals(expectedResult));
}

例如:

if (AllEqual(4, 2 + 2, 2 * 2, 1 + 1 + 1 + 1)) {
    // true
}

if (AllEqual(4, 2 + 2, 2 * 3)) {
    // not true
}

这甚至适用于其他类型,例如

if (AllEqual("foo", "f" + "o" + "o", "fooo".Substring(0, 3))) {
    // true
}

你可以使用这个扩展方法

public static bool SameAs<T>(this T val, params T[] values) where T : struct
{
    return values.All(x => x.Equals(val));
}

if (4.SameAs(2*2,2+2)) { ... }

如果您想减少重复,Linq 查询非常有效:

IEnumerable<int> fourExpressions = new List<int>{ 2 + 2, 2 * 2, ... };
bool allEqualToFour = fourExpressions.All(x => x == 4);
if (allEqualToFour) { ... }

或一行

if (new int[]{ 2 + 2, 2 * 2 }.All(x => x == 4)) { ... }

或(ab)使用扩展方法来获得最大的简洁性。 (通常,用这样的辅助方法污染所有对象并不是最好的主意,所以这个方法最好保留为静态方法。)

public static class QuestionableExtensions
{
    public static bool EqualsAllOf<T>(this T value, params T[] collection) where T : IEquatable<T>
    {
        return collection.All(t => value.Equals(t));
    }
}

public class MyClass 
{
    public void MyMethod()
    {
        if (4.EqualsAllOf(2 * 2, 2 + 2)) { ... }
    }
}

下面的 page 有一个很好的链接集合,解释了 Linq 查询。

参考@walpen 的回答,指出 All 是一个 Linq 扩展(在 System.Linq.Enumerable 中定义)。

我想提供 All 的 .NET 2.0 实现作为扩展。

首先,我们将定义一个名为 ExtensionAttributeAttribute。 (所以参考:Using extension methods in .NET 2.0?

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class
         | AttributeTargets.Method)]
    public sealed class ExtensionAttribute : Attribute { }
}

然后,(通过魔法)我们可以使用 this 参考进行扩展。因此,我们将为 ExtensionAttribute 的用法创建一个 static class:我选择通过在 Enumerable 中定义一个名为 Enumerable 来实现此目的=23=]命名空间。

namespace System.Collections.Generic
{
    public static class Enumerable
    {
        //[System.Runtime.CompilerServices.Extension()]
        public static bool All<T>(this IEnumerable<T> list, T expectedAnswer)
            where T : IEquatable<T>, IComparable<T>
        {
            bool result = true;    
            IEnumerator<T> enumerator = list.GetEnumerator();

            while (enumerator.MoveNext())
            {
                result = result && (Object.Equals(enumerator.Current, expectedAnswer));

                if (!result)
                    return result;
            }
            return result;
        }

        public delegate bool MyFunc<T>(T next);


        public static bool All<T>(this IEnumerable<T> list, MyFunc<T> fn)
            where T : IEquatable<T>, IComparable<T>
        {
            bool result = true;    
            IEnumerator<T> enumerator = list.GetEnumerator();

            while (enumerator.MoveNext())
            {
                result = result && fn.Invoke(enumerator.Current);

                if (!result)
                    return result;
            }
            return result;
        }
    }
}

那么,使用起来就很简单了

IEnumerable<int> ints = new int[] { 2 + 2, 2 * 2, 1 * 3 + 1 };
Console.Write("Result := {0}", ints.All(4)); // outputs "Result := true"
Console.Write("Result := {0}", ints.All(t => t.Equals(4))); // outputs "Result := true"