c# - If 语句的更好语法

标签 c# .net if-statement

我有这样的情况:

if (string.IsNullOrEmpty(filename) || size != "Large" || size != "Medium" || size != "Small")

将来我可能不得不在 if 语句中管理更多的 size

我想知道是否存在一种更易于管理和可读的方式来编写此条件。

请提供一个真实的例子,感谢您抽出宝贵的时间。

最佳答案

您可以保留一个单词哈希集并检查:

HashSet<string> filterWords = new HashSet<string>();
// Put all words in the hash set


if (filterWords.contains(size))
{
    // Do what ever you need
}

关于c# - If 语句的更好语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7077182/

相关文章:

c# - 如何使用c#找出程序的路径

c# - 在 C# 接口(interface)和接口(interface)的方法签名之一中实现枚举

c# - 在 C# 中捕获 RAISERROR

.net - 如何为 Entity Framework 创建三层解决方案

MySQL IF...THEN 语法错误

javascript - 有没有更好的方法来避免 if/then/else?

c# 如何建立UDP套接字连接

c# - .net Winforms 中的保存图像问题

c# - 如何将文本拆分为单词?

PHP if 或语句不工作