Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#TextBox.Text=多个单词_C# - Fatal编程技术网

C#TextBox.Text=多个单词

C#TextBox.Text=多个单词,c#,C#,我已经创建了一个计时器,它会检查文本框中键入的内容,如果键入的“写入密码”更改了图片,那么我的另一个if函数不起作用,我该如何生成这样的内容: 语句的代码,我需要这样的代码:if(metroTextBox1.Text==“byby”、“cow”、“root”) 试试这个: if(new string[] { "byby", "cow", "root" }.Contains(metroTextBox1.Text)) { ... } 编辑: 与注释中建议的一样,您可以使用哈希集而不是数组来存

我已经创建了一个计时器,它会检查文本框中键入的内容,如果键入的“写入密码”更改了图片,那么我的另一个if函数不起作用,我该如何生成这样的内容:

语句的代码,我需要这样的代码:
if(metroTextBox1.Text==“byby”、“cow”、“root”)

试试这个:

if(new string[] { "byby", "cow", "root" }.Contains(metroTextBox1.Text))
{
   ...
}
编辑:

与注释中建议的一样,您可以使用
哈希集
而不是
数组
来存储要比较的单词。
Contains
方法使用
HashSet
工作得更快,因为它有
O(1)
查找,而
数组和
列表有
O(n)
查找

HashSet<string> words = new HashSet<string>(){ "byby", "cow", "root" };
if (words.Contains(metroTextBox1.Text))
{
    ...
}
HashSet words=newhashset(){“byby”、“cow”、“root”};
if(words.Contains(metroTextBox1.Text))
{
...
}
试试这个:

if(new string[] { "byby", "cow", "root" }.Contains(metroTextBox1.Text))
{
   ...
}
编辑:

与注释中建议的一样,您可以使用
哈希集
而不是
数组
来存储要比较的单词。
Contains
方法使用
HashSet
工作得更快,因为它有
O(1)
查找,而
数组和
列表有
O(n)
查找

HashSet<string> words = new HashSet<string>(){ "byby", "cow", "root" };
if (words.Contains(metroTextBox1.Text))
{
    ...
}
HashSet words=newhashset(){“byby”、“cow”、“root”};
if(words.Contains(metroTextBox1.Text))
{
...
}

好的,我会在Slaven Tojić的回答中加上我的2美分:

  • 您可以创建包含单词集合的属性:

    private HashSet<string> WordsList { get; } = new HashSet<string>(new[]
    {
        "byby",
        "cow",
        "root"
    });
    

  • 在事件处理程序中,使用集合检查它是否包含 要素:


好的,我将在Slaven Tojić的回答中加上我的2美分:

  • 您可以创建包含单词集合的属性:

    private HashSet<string> WordsList { get; } = new HashSet<string>(new[]
    {
        "byby",
        "cow",
        "root"
    });
    

  • 在事件处理程序中,使用集合检查它是否包含 要素:


将其与比较器一起使用,以避免案例问题

      if(new string[] { "byby", "cow", "root" }
         .Contains(metroTextBox1.Text,StringComparison.OrdinalIgnoreCase))
        {
           ...
        }

与Comparator一起使用,以避免案例问题

      if(new string[] { "byby", "cow", "root" }
         .Contains(metroTextBox1.Text,StringComparison.OrdinalIgnoreCase))
        {
           ...
        }

您可以使用
.Contains
function@Arturthetemplar关于
“单词列表”。包含(“或”)
?@DavidG你是对的,对不起:^)我有一个文本框,用于检查写入的文本,我可以将其设置为检查单词是否为“cow”、“kitchen”、“tree”吗在一个if语句中?您可以使用
.Contains
function@Arturthetemplar关于
“单词列表”。包含(“或”)
?@DavidG你是对的,对不起:^)我有一个文本框,用于检查写入的文本,我可以让它在一个if语句中检查单词是否为“cow”、“kitchen”、“tree”?@Arturthetemplar否不会。它检查整个单词,也考虑将数组移动到一个字段,以避免多次实例化。也可以考虑使用<代码>哈什特集< /代码>。它检查整个单词,也考虑将数组移动到一个字段,以避免多次实例化。也可以考虑使用<代码> HasSET/<代码>。我相信<>代码> STRIGATION比较在您的<代码>中。包含应该是代码> StringComparer < /代码> .TiMyTyg。比较时只需忽略大小写,因此不需要编写比较器。我认为
中的
StringComparison
应该是
stringcomparier
@TimothyG。比较时只需忽略大小写,因此不需要编写比较器