Static Indexer - Why Not?

  • Thread starter Thread starter Bill Cohagan
  • Start date Start date
B

Bill Cohagan

I'm curious as to why C# doesn't support static indexers. Anybody know?

Thanks,
Bill
 
Indexer semantics require the 'this' keyword which defines the block of code
as an indexer, and is a also reference to the current instance of a class.
Since a static indexer would have no such reference, it stands to reason that
you can't define an indexer as static. That's just my personal
interpretation, there may be a bigger picture than that.

However, if you have a special need, indexers are just a convenience - you
can accomplish what you want to do the old fashioned way through methods.

The fact of the matter is, however, that indexers can't be defined as static.
 
Peter Bromberg said:
Indexer semantics require the 'this' keyword which defines the block of code
as an indexer, and is a also reference to the current instance of a class.
Since a static indexer would have no such reference, it stands to reason that
you can't define an indexer as static. That's just my personal
interpretation, there may be a bigger picture than that.

That's just a matter of the C# team not having defined static indexer
syntax. There's nothing *logically* to stop C# from having indexers.
However, if you have a special need, indexers are just a convenience - you
can accomplish what you want to do the old fashioned way through methods.

The fact of the matter is, however, that indexers can't be defined as static.

.... in C#. I believe the CLR supports it.

There are times I'd have liked it too, to be honest.
 
Jon Skeet said:
... in C#. I believe the CLR supports it.

There are times I'd have liked it too, to be honest.

That would mean your static class is starting to take state, which probably
means it should be singleton instead?

Michael
 
I wasn't asking about static classes as any class can have static members.
The concept of a static (or singleton) class is distinct from that of a
class that happens to have static members.

For instance in my case I have a set of classes representing business
objects. Our convention is that for each such class Foo we define a static
property, Members, of type List<Foo> that serves as a container for all of
the instances of Foo. What we're doing is prototyping a system and using the
Members static variables as substitutes for a database. I had wanted to be
able to write Foo[index] rather than having to write Foo.Members[index],
just as a convenience. Having to resort to a static method just changes the
name from Members to, say, Get and swaps the [] for (), not much of an
improvement.

Bill
 
Bill Cohagan said:
I wasn't asking about static classes as any class can have static members.
The concept of a static (or singleton) class is distinct from that of a
class that happens to have static members.

Perhaps I chose my words poorly but I was seperating the static
functionality and the non-static functionality. You could look at it like
this:

ClassWithStaticMethods = NonStaticClass + StaticClass

ie, you can seperate the concept of the static class and non static class.
Make sense? In this particular discussion whether the class has non static
methods or not is irrelevant because we are discussing the static
functionality of that class. The static part of your class probably
shouldn't have state because this makes it like an object so should possibly
be a singleton class.

Michael
 
Michael C said:
That would mean your static class is starting to take state, which probably
means it should be singleton instead?

Not necessarily. Take Encoding.GetEncoding, for instance. That could
easily be an indexer instead.

(There's no need for it to be a static class, either - again, Encoding
is a good example.)
 
Michael C said:
Perhaps I chose my words poorly but I was seperating the static
functionality and the non-static functionality. You could look at it like
this:

ClassWithStaticMethods = NonStaticClass + StaticClass

ie, you can seperate the concept of the static class and non static class.
Make sense? In this particular discussion whether the class has non static
methods or not is irrelevant because we are discussing the static
functionality of that class. The static part of your class probably
shouldn't have state because this makes it like an object so should possibly
be a singleton class.

This suggests that you think there should never been any static
variables of any class (which would make a singleton difficult to start
with). Do you really never have any static variables?
 
Jon Skeet said:
This suggests that you think there should never been any static
variables of any class (which would make a singleton difficult to start
with). Do you really never have any static variables?

It's rare but I do have them. I wouldn't implement an entire collection as
static though.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top