1

Currently my code is like that

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

And am calling that as

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

But i want to call like this

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

How should i modify my class to do so please help me out

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Nitesh Shaw
  • 216
  • 2
  • 17

4 Answers4

4

I think you are going really bad here since using an indexer to call a method is just wrong. And with that, you can't have indexers on static classes.

That said, this is how it does work:

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

I have used an inner class to create a helper, which creates your property name. Inside there is an indexer that has your original code.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

Not sure why you would want to do it that way, but:

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

It must be called with an instance:

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

Unfortunately you cannot do it with a static member. Alternatively you need to create an indexer inside an auxillary class that's instance name is IsExist.

My opinion is that you should leave it as it is.

Jim
  • 14,952
  • 15
  • 80
  • 167
1

To achieve it exactly the way you want it (even though in my opinion it isn't a good design) use an inner class that will override the [] operator and will check your condition. Then in your original class have a property of that inner class.

Keep in mind that when overriding the [] operator you can't be using a static class.

See that instead of using Any you can just use Contains - because you are checking for the entire object itself it is a cleaner way to do so

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

Use:

var doesItContain = ExampleClass.IsExist["b"]; // false
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
1

Currently, you are calling a function and its as simple as declaring it, but for square brackets, you will have to overload them. Here is a helpful link: How do I overload the square-bracket operator in C#?

Basically, in your case it should look like this:

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

I haven't tested the code, so let me know if it works.

Community
  • 1
  • 1
Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41