IanG on Tap

Ian Griffiths in Weblog Form (RSS 2.0)

Blog Navigation

April (2018)

(1 item)

August (2014)

(1 item)

July (2014)

(5 items)

April (2014)

(1 item)

March (2014)

(1 item)

January (2014)

(2 items)

November (2013)

(2 items)

July (2013)

(4 items)

April (2013)

(1 item)

February (2013)

(6 items)

September (2011)

(2 items)

November (2010)

(4 items)

September (2010)

(1 item)

August (2010)

(4 items)

July (2010)

(2 items)

September (2009)

(1 item)

June (2009)

(1 item)

April (2009)

(1 item)

November (2008)

(1 item)

October (2008)

(1 item)

September (2008)

(1 item)

July (2008)

(1 item)

June (2008)

(1 item)

May (2008)

(2 items)

April (2008)

(2 items)

March (2008)

(5 items)

January (2008)

(3 items)

December (2007)

(1 item)

November (2007)

(1 item)

October (2007)

(1 item)

September (2007)

(3 items)

August (2007)

(1 item)

July (2007)

(1 item)

June (2007)

(2 items)

May (2007)

(8 items)

April (2007)

(2 items)

March (2007)

(7 items)

February (2007)

(2 items)

January (2007)

(2 items)

November (2006)

(1 item)

October (2006)

(2 items)

September (2006)

(1 item)

June (2006)

(2 items)

May (2006)

(4 items)

April (2006)

(1 item)

March (2006)

(5 items)

January (2006)

(1 item)

December (2005)

(3 items)

November (2005)

(2 items)

October (2005)

(2 items)

September (2005)

(8 items)

August (2005)

(7 items)

June (2005)

(3 items)

May (2005)

(7 items)

April (2005)

(6 items)

March (2005)

(1 item)

February (2005)

(2 items)

January (2005)

(5 items)

December (2004)

(5 items)

November (2004)

(7 items)

October (2004)

(3 items)

September (2004)

(7 items)

August (2004)

(16 items)

July (2004)

(10 items)

June (2004)

(27 items)

May (2004)

(15 items)

April (2004)

(15 items)

March (2004)

(13 items)

February (2004)

(16 items)

January (2004)

(15 items)

Blog Home

RSS 2.0

Writing

Programming C# 5.0

Programming WPF

Other Sites

Interact Software

Bizarre Indexer Syntax in C#

Tuesday 15 June, 2004, 06:17 PM

C# allows you to define an 'indexer', a special kind of property that allows an array-like syntax to be used on instances of the class. For example, it's what lets the Hashtable support this kind of syntax:

myHashTable["foo"] = "bar";

This client syntax is all well and good. The part that I think looks a bit curious is the syntax with which classes implement an indexer:

public string this [string x]
{
    get { ... }
    set { ... }
}

The use of square brackets around the parameter list makes sense, as that reflects the usage. The thing that I always thought looked a bit peculiar was the use of the this keyword.

The only way I was ever able to make sense of this was by guessing that at some point in its history, C# supported parameterisation of all properties. The CLR supports this, as does VB.NET, but C# today does not, with the specific exception of indexers. (Indexers are really parameterised properties with special support in certain languages.) However, it's not hard to imagine a variation on the indexer syntax that would allow you to define a parameterized property:

public string MyProperty [string x]
{
    get { ... }
    set { ... }
}

Given that more general usage, the use of this to indicate the default indexer property seems much less arbitrary. But I'd never seen any confirmation of this guess until Brad posted this blast from the past. In passing he shows an ancient bit of code which uses exactly the syntax I had long suspected existed once upon a time.

What's still not clear is why this was removed from the language. My guess is that the client-side syntax is arguably ambiguous:

// Is Foo a parameterised property, or a scalar property
// returning an object which has an indexer?

someObject.Foo["foo"] = "quux";

...but if anyone knows for sure I'd love a definitive answer.

Copyright © 2002-2013, Interact Software Ltd. Content by Ian Griffiths. Please direct all Web site inquiries to webmaster@interact-sw.co.uk