Do any #DotNet folks know why we still can't create static indexers for classes in C#?
What exactly are you looking for when you say "static indexing", do you mean syntatically Foo["Bar"] but dont care if in terms of implementation it is effectively the same thing... or do you mean an immutable index defined at compile time? If so how is that useful in any way as opposed to just an immutable index that is created at run time? I assume your trying to address a performance concern with generating the index whent he program first spins up?
In C#, we can define custom indexers like so:
public object this[int index] { get { etc } }
That are then invoked like this:
objectRef[i]
I just want to do the exact same thing, except static:
public static object this[int index] { get { etc } }
That would be invoked like this:
ClassName[i]
This would be particularly useful for central data repositories using the singleton pattern, and it's really no different, functionally, from static methods.
Really, what it comes down to is right now, I have to do ClassName.Singleton[id], but I'd like to shorten that.
Ahh so its a syntax limitation. You would effectively like a shorter syntax that would otherwise be functionally the same as a longer one.
Interesting. Yea I cant think of a good reason a language would prevent that.
@freemo Yeah, but you could easily get around it by replacing "this" in that static example with either the class name or the "operator" keyword. Either would make syntactic sense in this scenario.
@LouisIngenthron Oh I agree entierly.. its a weak excuse. Just the only one I can fathom one would argue at all.
I m,ean its clearly syntatical and not functional in nature cause we are basically just treating [] like a function. We are entierly int he land of syntatic sugar, so any explanation is going to be one that is purely hand waving.