【问题标题】:I want to use `[]` to check if a string exists in an array or not我想使用`[]`来检查一个字符串是否存在于数组中
【发布时间】:2016-08-25 09:24:51
【问题描述】:

目前我的代码是这样的

public class ExampleClass
{
    private static string[] words = new string[] { "Word1","Word2","Word3","Word4","Word5" };

    public static bool IsExist(string Word)
    {
        return words.Any(w => w == Word);
    }
}

我称之为

ExampleClass.IsExist("Word1"); //Returns true
ExampleClass.IsExist("WordNotExist"); //Returns false

但我想这样打电话

ExampleClass.IsExist["Word1"]; //Returns true
ExampleClass.IsExist["WordNotExist"]; //Returns false

我应该如何修改我的课程,请帮帮我

【问题讨论】:

  • 你为什么要那个?它不是一个数组,对吧?
  • 我看不出有什么原因,你不能在静态类中声明索引属性。
  • 没问题,我的代码完全不同。请给我任何想法我必须做些什么来完成这个

标签: c#


【解决方案1】:

我认为你在这里真的很糟糕,因为使用索引器调用方法是错误的。这样一来,您就不能在静态类上使用索引器了。

也就是说,它是这样工作的:

public class ExampleClass
{
    public class IsExistHelper
    {
        private static string[] words = new string[] { "Word1", "Word2", "Word3", "Word4", "Word5" };

        public bool this[string Word]
        {
            get
            {
                return words.Any(w => w == Word);
            }
        }
    }

    public static IsExistHelper IsExist { get; } = new IsExistHelper();
}

我使用了一个内部类来创建一个帮助器,它会创建您的属性名称。里面有一个索引器,里面有你的原始代码。

【讨论】:

    【解决方案2】:

    不知道你为什么要这样做,但是:

    public class ExampleClass
    {
        private string[] words = new string[] { "Word1", "Word2", "Word3", "Word4", "Word5" };
    
        public bool this[string Word]
        {
            get { return words.Any(w => w == Word); }
        }
    }
    

    必须用实例调用:

    var _ = new ExampleClass();
    var isTrue = _["Word1"] == true
    

    很遗憾,您不能static 成员进行此操作。或者,您需要在实例名称为 IsExist 的辅助类中创建一个索引器。

    我的意见是你应该保持原样。

    【讨论】:

      【解决方案3】:

      要完全按照您想要的方式实现它(即使在我看来这不是一个好的设计),请使用一个内部类,该类将覆盖 [] 运算符并检查您的条件。然后在您的原始类中具有该内部类的属性。

      请记住,在覆盖 [] 运算符时,您不能使用静态类

      请注意,您可以使用Contains 而不是使用Any - 因为您正在检查整个对象本身,这是一种更简洁的方法

      public static class ExampleClass
      {
      
          public class InnerIsExist
          {
              private string[] words = new string[] { "Word1", "Word2", "Word3", "Word4", "Word5" };
      
              public bool this[string word]
              {
                  get {  return words.Contains(word); }
              }
          }
      
          public static InnerIsExist IsExist { get; } = new IsExistClass();
      }
      

      用途:

      var doesItContain = ExampleClass.IsExist["b"]; // false
      

      【讨论】:

        【解决方案4】:

        目前,您正在调用一个函数,它就像声明它一样简单,但是对于方括号,您将不得不重载它们。这是一个有用的链接:How do I overload the square-bracket operator in C#?

        基本上,在您的情况下,它应该如下所示:

        public bool this[string Word]
        {
            get { return words.Any(w => w==Word); }
        }
        

        我还没有测试过代码,所以请告诉我它是否有效。

        【讨论】:

          猜你喜欢
          • 2014-08-09
          • 2020-07-06
          • 1970-01-01
          • 2012-05-09
          • 2019-05-17
          • 1970-01-01
          • 1970-01-01
          • 2021-12-27
          相关资源
          最近更新 更多