在控制台应用程序中接受多个单词的存储

Store with multiple words accepted in Console Application

我存储了一个包含多个单词的字符串(接受任何选定的单词)。但是,我收到一条错误消息:

Invalid expression term

接受多个单词的正确存储方式是什么?

string correctDeviceSerialId = ["AEVL2020", "AEVL2021", "AEVL2022" ];
string correctUserId = "";                                            
string repeatDeviceSerialId = "";
while (repeatDeviceSerialId != correctDeviceSerialId)
{
    Console.Write("Enter your Device Serial: ");
    repeatDeviceSerialId = Convert.ToString(Console.ReadLine());
}

What is the correct way to store with multiple words accepted?

您数组中的声明存在语法错误。

替换

string correctDeviceSerialId = ["AEVL2020", "AEVL2021", "AEVL2022" ];

string[] correctDeviceSerialId = { "AEVL2020", "AEVL2021", "AEVL2022" };

这是

的快捷方式
string[] correctDeviceSerialId = new string[] { "AEVL2020", "AEVL2021", "AEVL2022" };

代码中几乎没有错误。我已经修复它们并在上面添加了评论。

// WRONG: string correctDeviceSerialId = ["AEVL2020", "AEVL2021", "AEVL2022" ];
// You should have a string array instead of string
string[] correctDeviceSerialId = { "AEVL2020", "AEVL2021", "AEVL2022" }; 
string correctUserId = "";                           
string repeatDeviceSerialId = "";

// WRONG: while (repeatDeviceSerialId != correctDeviceSerialId)
// You were trying to compare string array of strings.
// If you'd like to just check if the string contains in your correctDeviceSerialId
while (!correctDeviceSerialId.Contains(repeatDeviceSerialId))
{
    Console.Write("Enter your Device Serial: ");
    repeatDeviceSerialId = Convert.ToString(Console.ReadLine());
}

P.S。也不要忘记将 using System.Linq 添加到文件的顶部