We do have generic classes, methods and delegates. My question is: what
reason prevents us from having generic properties and indexers?
// impossible
public List<T> GetList<T>
{
get
{
...
}
}
// impossible
public List<T> this<T>[string listName]
{
get
{
...
}
}
If you ask where such syntax could be handy, then this is concrete scenario:
suppose you need a class that contains a list of generic lists where each
generic list is instantiated with different type.
class ListContainer
{
// can't store list of generic lists in List<> but arraylist is enough
ArrayList lists;
}
class ListContainer
{
public void AddList<T>( List<T> list )
{
lists.Add( list );
}
}
Now you would like a generic indexer...
public List<T> this<T>[ string listName ]
{
get
{
...
....but have to use a method instead
public List<T> GetList<T>( string listName )
{
...
the same applies to properties - you have to use generic methods since
generic properties are not available.
I would be glad to hear your opinion on this issue.
Regards,
Wiktor Zychla