您好我正在使用c#编写一个简单的翻译器,我有一个英文单词列表。如何让程序检查用户输入的单词并用列表中的单词检查它们,而不用带有一堆&&的if语句。
请帮忙吗?
答案 0 :(得分:3)
使用List对象的Contains方法。
答案 1 :(得分:0)
易:
答案 2 :(得分:0)
示例:(对不起,这是@Stephane Mathis回答)
public static class SqlServerKeywords {
public static readonly HashSet<string> SqlServerReservedKeywords = new HashSet<string> {
"ADD",
"EXISTS",
"PRECISION",
"ALL",
"EXIT",
"PRIMARY",
"ALTER",
"EXTERNAL",
"PRINT",
"AND",
"FETCH",
"PROC",
"ANY",
"FILE",
"PROCEDURE",
"AS",
"FILLFACTOR",
"PUBLIC",
"ASC",
"FOR"
};
}
public class OtherClass {
private bool Exists (string myWord) {
var exists = SqlServerReservedKeywords.Contains(myWord);
return exists;
}
}