What about just extending List<T>This is a workable solution, however:
class Column Collection : List<Column>
{
Column this[string someName]
{
get
{
foreach(Column col in this)
{
if (string.Equals(col.ColumnName, someName,
StringComparison.Ordinal))
return col;
}
return null;
}
}
}
- It doesn't implement IDictionary<string, Column>, so it can't be passed to
methods that operate on dictionaries, even though this container logically
is one. Of course, this is a relatively minor issue if you never plan on
doing that anyway.
- It doesn't scale for many columns or for many lookups, since lookup takes
time linear to the number of columns (rather than near-constant time if a
dictionary is used). This may not be an issue if you only ever plan to use
it for a small number of columns, but then you should clearly document that
this is how the class should be used.
- Because it extends List rather than aggregating it, it can't maintain
additional invariants. In particular, it's possible to add a column with the
same name twice. The lookup by name will only ever return one of these
elements. This is unlikely to be what you want.
--
J.