S
Sergey Klementiev
Why it's impossible to have a static indexer in C#?
Bob Powell said:An indexer will access some instance data. Static methods and properties may
not access instance data so there's no point in having one.
Eric Gunnerson said:Indexers are intended as a way to make your instance behave as if it was an
array, and static indexers are outside of that usage.
They could be added, but we'd have to be convinced that their utility was
worth the additional complexity. What do you want to use them for?
Eric Gunnerson said:Indexers are intended as a way to make your instance behave as if it was an
array, and static indexers are outside of that usage.
They could be added, but we'd have to be convinced that their utility was
worth the additional complexity. What do you want to use them for?
Daniel O'Connell said:The immediate usage that comes to mind, for me, are static classes somewhat
simliar to lookup tables I used to use in C, a global, static, readonly
array. A type with a static indexer could be considered as a static array, a
piece of memory that simply exists with a set of values in it. I can see it
conceptually. Although the functionality can easily be managed using a
static array or IList property, there is the question of why a static type
cannot appear as an array, after all there are static methods, static
properties, static events, etc etc, a static can look like an instance
object in most other respects, I wonder if there is a reason one shouldn't
be allowed to act as an array, when appropriate.
Mind you, this is an issue of conceptuallity, and I would need a fair bit of
convincing myself that the feature is really useful(or proper, but thats a
whole nother can of worms), I just can't find a reason why the concept
doesn't work.
Jon Skeet said:As a concept I'm sure it does work. I believe what Eric was saying
(correct me if I'm wrong, Eric!) is that the occasionally useful time
isn't worth the extra complexity of language in terms of syntax not
only for declaring it in the first place (static this doesn't sound
good, IMO, even if it's the logical extension) but also accessing the
indexer when it's set up. If no other languages support it, that would
also be a problem - it's much less useful to have a feature which only
C# can access.
I think named indexers are a far more pressing issue, myself.
Justin Rogers said:I think the feature is more than occasionally useful. However, I don't feel
that there aren't ways to simply work around, and easily work around, the times
where you might use the feature. However, in my case, I tend to think named
indexers have the same issue. A named indexer is easy to work around by extending
a property of a given class that is itself indexed.
The largest use for static indexers would be as factories.
Agreed.
I'm up in the air on this one. Features are nice, but I'm all about the
performance. If adding named indexers or static indexers in at the language level
is going to be faster and more performant that me using a class with a default
indexed property or a static creation function
backed by a Hashtable then give it to me, because I want the speed. In this
case the only feature I can see actually being faster is the named indexer since
it'll get rid of a property
look-up (that I could have made a public read-only field anyway).
Eric Gunnerson said:Yes, that's pretty much what I meant. I'm trying to understand how people
would use them and what they're using now instead of static indexers to
figure out how useful people would find them.
If that makes sense...