C#比较速记 [英] C# Comparison shorthand
本文介绍了C#比较速记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的代码:
如果(Y == A和&安培; Y == B和;&安培; Ÿ== C&功放;&安培; Y = D ......)
{
:
}
是否有某种形式的简写,这样我可以把它改写为这样的事?
如果(Y ==(A和和b&放大器;和C&放大器;和D ...))
{
:
}
的功能应该是完全一样的。我只是在寻找的东西看起来不太令人困惑。
修改对不起,不澄清,所有的变量都是整数。我在寻找一个较短的方法,以确保 A
, B
, C
, D
,...一律平等是
。
< DIV CLASS =h2_lin>解决方案
您会得到(没有实现自己的一种机制)最接近的:
如果(新[] {A,b,C,D}。所有(价值=> Y ==值))
// ...
I have this code:
if (y == a && y == b && y == c && y == d ...)
{
...
}
Is there some form of shorthand so that I can rewrite it as something like this?
if(y == (a && b && c && d ...))
{
...
}
The functionality should be exactly the same. I'm just looking for something that looks less confusing.
EDIT Sorry for not clarifying, all the variables are integers. I'm looking for a shorter way to ensure that a
, b
, c
, d
, ... all equal y
.
解决方案
The closest you're going to get (without implementing your own kind of mechanism):
if (new[] { a, b, c, d }.All(value => y == value))
// ...
这篇关于C#比较速记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文