Equality comparison between multiple variables

前端 未结 14 1284
梦毁少年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:30

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

    public bool EqualsToAll(this T element, IEnumerable 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.

提交回复
热议问题