Do any folks know why we still can't create static indexers for classes in C#?

@LouisIngenthron

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?

@freemo

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

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.

Follow

@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.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.