多变量之间比较平等 [英] Equality comparison between multiple variables

查看:143
本文介绍了多变量之间比较平等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况我需要检查多个变量是否有诸如

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;

我想要查询是否X == 1和y == 1 Z == 1(它可以是1或一些其它值)。取而代之的是,有没有什么简便方法,我可以实现相同的,如下面

I want to check whether x==1 and y==1 z==1 (it may be '1' or some other value). instead of this, is there any short way I can achieve same such as below

if(x==y==z==1)

这是可能在C#?

Is this possible in C#?

推荐答案

KennyTM是正确的,就没有其他更简单或更有效的方式。

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

不过,如果你有很多的变量,你也可以建立一个值数组,并使用IEnumerable.All方法来验证他们都是1.更具可读性,海事组织。

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))

而不是

if(v1 == 1 && v2 == 1 && v3 == 1 && v4 == 1 && v5 == 1 && v6 == 1 && v7 == 1 && v8 == 1 && v9== 1 && v10 == 1)

这篇关于多变量之间比较平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆