Equality comparison between multiple variables

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

    Add this extension:

    public static class Extensions
    {
        public static bool EqualsAll(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))
    

提交回复
热议问题