Tuesday, November 27, 2007

Extending indexer on collection in C#

I was looking for a way to implement alternative way to access members of List (or any other collection) through index. By default i could use only:

myCol[0].Value

What i'd like to use is:

myCol[strKey].Value

The way to implement it is through property (code-fragment only):

public MyColItem this[string strKey]
{
get { return #code to get the item#; }
set { #your code#; }
}

No comments: