By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

Static Indexer - Why Not?

P: n/a
I'm curious as to why C# doesn't support static indexers. Anybody know?

Thanks,
Bill
Mar 5 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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.
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Bill Cohagan" wrote:
I'm curious as to why C# doesn't support static indexers. Anybody know?

Thanks,
Bill

Mar 5 '06 #2

P: n/a
Peter Bromberg [C# MVP] <pb*******@yahoo.nospammin.com> wrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 5 '06 #3

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
... 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
Mar 5 '06 #4

P: n/a
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

"Michael C" <no****@nospam.com> wrote in message
news:eV*************@TK2MSFTNGP15.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
... 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

Mar 6 '06 #5

P: n/a
"Bill Cohagan" <co*****@nospam.nospam> wrote in message
news:OS**************@TK2MSFTNGP11.phx.gbl...
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
Mar 6 '06 #6

P: n/a
Michael C <no****@nospam.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
... 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?


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 6 '06 #7

P: n/a
Michael C <no****@nospam.com> wrote:
"Bill Cohagan" <co*****@nospam.nospam> wrote in message
news:OS**************@TK2MSFTNGP11.phx.gbl...
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 6 '06 #8

P: n/a
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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
Mar 6 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.