B
Bill Cohagan
I'm curious as to why C# doesn't support static indexers. Anybody know?
Thanks,
Bill
Thanks,
Bill
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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.
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.
Jon Skeet said:... in C#. I believe the CLR supports it.
There are times I'd have liked it too, to be honest.
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.
Michael C said:That would mean your static class is starting to take state, which probably
means it should be singleton instead?
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.
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?
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.