Static Indexers?

I believe it was considered not to be terribly useful. I think it's a shame too - an example I tend to use is Encoding, where Encoding.GetEncoding("foo") could be Encoding["Foo"]. I don't think it would come up very often, but aside from anything else it just feels a little inconsistent not to be available.

I would have to check, but I suspect it's available in IL (Intermediate Language) already.


Indexer notation requires a reference to this. Since static methods don't have a reference to any particular instance of the class, you can't use this with them, and consequently you can't use indexer notation on static methods.

The solution to your problem is using a singleton pattern as follows:

public class Utilities
{
    private static ConfigurationManager _configurationManager = new ConfigurationManager();
    public static ConfigurationManager ConfigurationManager => _configurationManager;
}

public class ConfigurationManager
{
    public object this[string value]
    {
        get => new object();
        set => // set something
    }
}

Now you can call Utilities.ConfigurationManager["someKey"] using indexer notation.