作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否有更短版本的 IF 语句来执行此操作?
if (el.type == ElementType.Type1 || el.type == ElementType.Type2)
最佳答案
您可以使用扩展方法,但这真的会好得多吗?
把它放在一个静态类上:
public static bool IsOneOf(this ElementType self, params ElementType[] options)
{
return options.Contains(self);
}
然后你可以做:
if (el.type.IsOneOf(ElementType.Type1, ElementType.Type2)) {
但是,这将比您的 if 语句慢 很多,因为有一个隐式数组初始化,然后是一个数组遍历,而不是(最多)两个比较和分支。
关于C# - IF 语句的较短版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4399036/
我是一名优秀的程序员,十分优秀!