Equality comparison between multiple variables

前端 未结 14 1103
梦毁少年i
梦毁少年i 2020-11-29 08:00

I\'ve a situation where I need to check whether multiple variables are having same data such as

var x=1;
var y=1;
var z=1;

I want to check

相关标签:
14条回答
  • 2020-11-29 08:26
    var x = 1;
    var y = 1;
    var z = 1;
    
    if (AllEqual(1, x, y, z))    // true
    if (AllEqual(2, x, y, z))    // false
    if (AllEqual(x, y, z))       // true
    
    var a = 1;
    var b = 2;
    var c = 3;
    
    if (AllEqual(a, b, c))       // false
    
    // ...
    
    public static bool AllEqual<T>(params T[] values)
    {
        if (values == null)
            throw new ArgumentNullException("values");
    
        if (values.Length < 1)
            throw new ArgumentException("Values cannot be empty.", "values");
    
        T value = values[0];
        for (int i = 1; i < values.Length; i++)
        {
            if (!value.Equals(values[i]))
                return false;
        }
        return true;
    }
    
    0 讨论(0)
  • 2020-11-29 08:26

    I adapted Mau's solution into an extension method. It would be nice if they added this to the framework for all value types.

    public static class IntegerExtensions
    {
        public static bool EqualsAll(this int subject, params int[] values)
        {
            if (values == null || values.Length == 0)
            {
                return true;
            }
    
            return values.All(v => v == subject);
        }
    }
    
    0 讨论(0)
  • 2020-11-29 08:29

    KennyTM is correct, there is no other simpler or more efficient way.

    However, if you have many variables, you could also build an array of the values and use the IEnumerable.All method to verify they're all 1. More readable, IMO.

    if (new[] { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 }.All(x => x == 1))
    

    Instead of

    if(v1 == 1 && v2 == 1 && v3 == 1 && v4 == 1 && v5 == 1 && v6 == 1 && v7 == 1 && v8 == 1 && v9== 1 && v10 == 1)
    
    0 讨论(0)
  • 2020-11-29 08:30

    If you just want to testif x == y == z you can use:

    var allEqual = new[] {x, y, z}.Distinct().Count() == 1;
    

    If you want to test if they're all equal to 1, add 1 to the set:

    var allEqual1 = new[] {x, y, z, 1}.Distinct().Count() == 1;
    

    or use All as in fencliff's answer.

    0 讨论(0)
  • 2020-11-29 08:30

    Actually i don't have to the time to code, but an extension method with linq like this

    public bool EqualsToAll<T>(this T element, IEnumerable<T> source)
    {
        if(element == null)
            throw new ArgumentNullException(element);
    
        foreach(var item in source)
        {
            if(!element.Equals(item)
                return false;
        }
    
        return true;
    }
    

    should make it.

    Warning: This code was not tested, nor written within an IDE.

    0 讨论(0)
  • 2020-11-29 08:32

    Add this extension:

    public static class Extensions
    {
        public static bool EqualsAll<T>(this T subject, params T[] values) =>
            values == null || values.Length == 0 || values.All(v => v.Equals(subject));
    }
    

    Then call it like so:

    if(1.EqualsAll(x, y, z))
    
    0 讨论(0)
提交回复
热议问题