views:

56

answers:

2

Hi,

In .NET Framework, there are some classes which use SomethingCollection syntax.

For example, when dealing with SqlCommand, it has a parameter Parameters of type SqlParameterCollection. Since it does not have a form of IEnumerable<SqlParameter> (or IList<SqlParameter> or something similar), it is impossible to write:

foreach (var c in sqlCommand.Parameters)
{
    Debug.WriteLine(string.Concat(c.ParameterName, ": ", c.Value));
}

Instead, var must be replaced by an explicit SqlParameter.

The thing is very similar when dealing with Windows Forms controls (collections of listview items, etc.), and exists in other classes.

What is this thing? Why such classes exist and are used instead of generic IEnumerables/ILists?

+7  A: 

Many of the collections exposed by .NET APIs were designed before .NET 2.0, thus, before generics were available.

Adam Vandenberg
@Adam Vandenberg: so it's just something "very" old, and was not rewritten since the first version of .NET Framework?
MainMa
I would save the phrase "very" old for something like Cobol, but yes, a lot of the constructs that follow that pattern were in the original .NET 1.0 and 1.1 frameworks. Rewriting those components would be a major breaking change, so it probably best to let sleeping dogs lie.
Jason Whitehorn
@MainMa: You think it's a good idea to go and rewrite stuff just because you can when there is little/no benefit to the customer and you are effectively breaking a lot of their code?
Ed Swangren
A: 

It is possible that certain parts of the .net framework were designed by different teams with different goals in mind.

Some practitioners of Domain Driven Design would advocate the use of aggregations, which are root objects that control all the objects in the collections, as a form of a stop gap for some issues that arise from exposing collections.

I am guessing the parts of the .net framework that use this design had a reason for restricting access.

wllmsaccnt
@wllmsaccnt: note that using `SomethingCollection` syntax does not restrict access to the elements, nor forbid to change, add or remove them. It just makes it impossible to use `var` keyword.
MainMa
I have misunderstood the issue then. Thanks for letting me know.
wllmsaccnt