gpt4 book ai didi

C# - IF 语句的较短版本

转载 作者:行者123 更新时间:2023-11-30 13:34:03 24 4
gpt4 key购买 nike

是否有更短版本的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com