多条件测试的C#语法简写 [英] C# syntax shorthand for multiple condition testing

查看:51
本文介绍了多条件测试的C#语法简写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#中存在哪些语法来帮助进行多个条件测试?

What syntax exists in C# to help with multiple condition testing?

我经常必须通过以下方式测试多种情况:

I often have to test for multiple conditions in the following manner:

if (a == 3 || a == 4)

也许我是主观的,但这不是很漂亮.

Perhaps I'm being subjective, but that isn't very pretty.

如果存在更多条件,我可以这样做:

If there is a larger set of conditions, I could do:

if (new int[]{3, 4, 5, 6, 7}.Contains(a))

但是,如果只有几个条件需要测试,我不会保存任何击键.

But if there are just a few conditions to test, I'm not saving any keystrokes.

在C#语言中是否存在语法快捷方式,该快捷方式将使我能够完成以下操作,而无需进行许多击键操作,也无需扩展方法等?

Is there a syntax shortcut in the C# language that would allow me to accomplish something like the following, without many keystrokes and without extension methods, etc?

// doesn't work
if (a == 3 || 4)

这与该SO线程不同,因为它处理的是简短条件(2或3),所有条件在"a compare to b"比较中均具有不变的"a"值.

This is different from this SO thread, because it is dealing with short conditions (2 or 3), all with an unchanging "a" value in an "a compare to b" comparison.

推荐答案

没有.抱歉,但这是您的答案.

No there is not. Sorry, but this is your answer.

这篇关于多条件测试的C#语法简写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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