Do any #DotNet folks know why we still can't create static indexers for classes in C#?
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.
@LouisIngenthron I guess the only reason that makes sense is that the format for doing it... like "this" makes no sense in the static context.... but why the word "this" is needed at all seems syntatic itself so seems the only reasoning is "this unusual syntax we choose is confusing in that scenario and we dont want to change the pattern"
@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.
@LouisIngenthron
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.