Static Indexers?
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.
Are static indexers not supported in C#?
No, static indexers aren't supported in C#. Unlike other answers, however, I see how there could easily be point in having them. Consider:
Encoding x = Encoding[28591]; // Equivalent to Encoding.GetEncoding(28591)
Encoding y = Encoding["Foo"]; // Equivalent to Encoding.GetEncoding("Foo")
It would be relatively rarely used, I suspect, but I think it's odd that it's prohibited - it gives asymmetry for no particular reason as far as I can see.
Why no static indexers?
That's a good dupe reference there. Specifically, check out the answer :-)
Static Indexers?
Related Topics
How to Find Default Web Browser Using C#
Get Property Name and Type Using Lambda Expression
Async Process Start and Wait for It to Finish
Wpf Treeview Hierarchicaldatatemplate - Binding to Object with Multiple Child Collections
On Postback, How to Check Which Control Cause Postback in Page_Init Event
How to Add an Extra Button to the Window's Title Bar
Generic Method Multiple (Or) Type Constraint
How to Find the "Clamp" Function in .Net
Ef Code First: How to Get Random Rows
Creating an Instance Using Ninject with Additional Parameters in the Constructor
C# Sending Mails with Images Inline Using Smtpclient
How to Copy Part of an Array to Another Array in C#
Aspect Oriented Programming in C#
How to Sum Up an Array of Integers in C#
How to Make C# Switch Statement Use Ignorecase
Custom App.Config Section with a Simple List of "Add" Elements