S h o r t S t o r i e s

// Tales from software development

Static indexers are not supported in C#

leave a comment »

C# is a comprehensive language that covers most of the needs of a typical application software developer but once in a while I find myself trying to implement something a little unusual and discovering that C# doesn’t allow it. Often there’s a good reason but sometimes there isn’t.

How about static indexers ? I’ve never needed one before but I have a static class that’s used to hold lookup data. I wanted to provide an indexer so that the data could be enumerated as well as accessed directly through individual properties.

If you try this you’ll quickly discover that static indexers aren’t supported because as soon as you omit the ‘this’ part of the indexer signature the compiler doesn’t have any idea of what you’re trying to do. You’ll get syntax error messages like “Array size cannot be specified in a variable declaration”.

Googling “c# static indexer” turned up a load of hits confirming that C# doesn’t support this but one particular thread provided some interesting comments from Microsoft’s Eric Gunnerson.

The bottom line ? The C# team didn’t think it was worth the effort…


Written by Sea Monkey

March 24, 2010 at 7:00 pm

Posted in Development

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: