C# 我想使用“[]”检查数组中是否存在字符串

C# 我想使用“[]”检查数组中是否存在字符串,c#,C#,目前我的代码是这样的 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); } } 我称之为 Example

目前我的代码是这样的

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

我应该如何修改我的类才能这样做请帮助我

不确定您为什么要这样做,但是:

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
不幸的是,无法使用
静态
成员执行此操作。或者,您需要在实例名为
IsExist
的辅助类中创建索引器


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

不确定你为什么要这样做,但是:

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
不幸的是,无法使用
静态
成员执行此操作。或者,您需要在实例名为
IsExist
的辅助类中创建索引器


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

我认为您在这里做得非常糟糕,因为使用索引器调用方法是错误的。这样,就不能在静态类上使用索引器

也就是说,这就是它的工作原理:

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();
}

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

我认为您在这里做得非常糟糕,因为使用索引器调用方法是错误的。这样,就不能在静态类上使用索引器

也就是说,这就是它的工作原理:

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();
}

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

要完全按照您想要的方式实现它(尽管我认为这不是一个好的设计),请使用一个内部类,该类将重写
[]
操作符并检查您的条件。然后在原始类中有一个内部类的属性

请记住,在重写
[]
运算符时,不能使用静态类

请注意,不要使用
任何
,您可以只使用
包含
——因为您正在检查整个对象本身,这是一种更干净的方法

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();
}
使用:


要完全按照您想要的方式实现它(尽管我认为这不是一个好的设计),请使用一个内部类,该类将重写
[]
操作符并检查您的条件。然后在原始类中有一个内部类的属性

请记住,在重写
[]
运算符时,不能使用静态类

请注意,不要使用
任何
,您可以只使用
包含
——因为您正在检查整个对象本身,这是一种更干净的方法

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();
}
使用:


当前,您正在调用一个函数,其简单程度与声明它一样,但是对于方括号,您将不得不重载它们。以下是一个有用的链接:

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

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

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

目前,您正在调用一个函数,它只需声明一个函数即可,但对于方括号,您必须重载它们。以下是一个有用的链接:

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

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

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

为什么要这样做?它不是数组,对吗?我看不出有什么原因,而且不能在静态类中声明索引属性。没问题,我的代码完全不同。请告诉我,我要做什么才能做到这一点,你为什么要这么做?它不是数组,对吗?我看不出有什么原因,而且不能在静态类中声明索引属性。没问题,我的代码完全不同。请告诉我要做些什么来完成这个任务