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:
Peter Bromberg [C# MVP] <pbromberg@yaho o.nospammin.com > wrote:[color=blue]
> 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.[/color]
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.
[color=blue]
> 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.[/color]
.... in C#. I believe the CLR supports it.
There are times I'd have liked it too, to be honest.
"Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
news:MPG.1e7562 833aebc4ac98cee 1@msnews.micros oft.com...[color=blue]
> ... in C#. I believe the CLR supports it.
>
> There are times I'd have liked it too, to be honest.[/color]
That would mean your static class is starting to take state, which probably
means it should be singleton instead?
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" <nospam@nospam. com> wrote in message
news:eVIKnPKQGH A.516@TK2MSFTNG P15.phx.gbl...[color=blue]
> "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
> news:MPG.1e7562 833aebc4ac98cee 1@msnews.micros oft.com...[color=green]
>> ... in C#. I believe the CLR supports it.
>>
>> There are times I'd have liked it too, to be honest.[/color]
>
> That would mean your static class is starting to take state, which
> probably means it should be singleton instead?
>
> Michael
>[/color]
"Bill Cohagan" <cohagan@nospam .nospam> wrote in message
news:OSEdNPMQGH A.2256@TK2MSFTN GP11.phx.gbl...[color=blue]
>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.[/color]
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:
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 C <nospam@nospam. com> wrote:[color=blue]
> "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
> news:MPG.1e7562 833aebc4ac98cee 1@msnews.micros oft.com...[color=green]
> > ... in C#. I believe the CLR supports it.
> >
> > There are times I'd have liked it too, to be honest.[/color]
>
> That would mean your static class is starting to take state, which probably
> means it should be singleton instead?[/color]
Not necessarily. Take Encoding.GetEnc oding, 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 <nospam@nospam. com> wrote:[color=blue]
> "Bill Cohagan" <cohagan@nospam .nospam> wrote in message
> news:OSEdNPMQGH A.2256@TK2MSFTN GP11.phx.gbl...[color=green]
> >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.[/color]
>
> 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:
>
> ClassWithStatic Methods = 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.[/color]
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 [C# MVP]" <skeet@pobox.co m> wrote in message
news:MPG.1e75f3 19b010e3f198cee 3@msnews.micros oft.com...[color=blue]
> 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?[/color]
It's rare but I do have them. I wouldn't implement an entire collection as
static though.
Comment