Why doesn't ICollection contain an Add method?

As in the title, does anyone know why the ICollection interface does not contain the Add method? It seems very strange that the general version of ICollection<T> has Add , but ICollection does not. Anyone with a deeper knowledge of this will be really helpful.

As for me, unfortunately, the developers who build SharePoint have never learned about generics, so each individual collection in the API is not a shared collection based on ICollection . I would like to add several extension methods to ICollection , which include, but are not limited to, adding to the collection, but this seems impossible (at least impossible without reflection).

EDIT:

Quite a lot of people talk about the reason, because ICollection.Add will require an Object and, therefore, will not be typeafe. This is not true. IList has an Add method that takes an Object . You just need to do typecheck and cast in the method that takes Object .

The argument that the array implements ICollection , and therefore it cannot have Add , also does not contain water. If ICollection had an Add method, it just needs to be explicitly implemented on arrays and throw an exception (like many arrays of arrays currently existing).

I really hoped that someone had a link to an explanation from one of the designers.

+7
source share
8 answers

It seems to me that naming interfaces is confusing. ICollection and ICollection<T> not part of the same inheritance chain - most collections simply implement both.

The documentation states what the interface does, so if you take it alone, you cannot expect Add to exist:

Defines the size, enumerations, and synchronization methods for all unrelated collections.

What I think? Personally, I think that this is either a direct game with names, or for the second time (when introducing common interfaces), the designers decided to put Add in ICollection<T> , because this time it was more common in this.

IList has Add and inherits ICollection , while IList<T> does not have Add and inherits ICollection<T> , which is like Add .

Drop it until the type hierarchy design evolves / matures.


As for extension methods, you can do something like:
 public static void AnotherMethod<T>(this ICollection<T> collection, T item) { } 

And use it this way:

 ICollection<string> s; s.AnotherMethod(""); 
+2
source

ICollection can be anything. It may be something that is nothing but an enumeration. There is no reason for the Add method, or even the Remove method to be. If you look at the interface more closely, it is almost completely readable. You can see how many elements are there, and you can list them. It. This makes perfect sense in an abstract way.

When we get to ICollection<T> , we are very specific right now. We know exactly what object it has, and therefore we can:

  • Add new <T> elements.
  • Find them using the IEquitable interface.
  • Delete them using the same methodology.

In essence, the difference is that ICollection<T> somewhat specific.

+1
source

From MSDN

You do not need to add collection types to known types when they are used polymorphically instead of other collections or collection interfaces. For example, if you declare a data member of type IEnumerable and use it to send an instance of ArrayList, you do not need to add an ArrayList to known types.

When using polymorphic collections instead of non-collection types, they must be added to known types. For example, if you declare an item of type Object and use it to send an instance of ArrayList, add an ArrayList to known types.

0
source

Based on Msdn definition , it

Determines the size, enumerations, and synchronization methods for all unrelated collections.

This means that ICollection represents the stream or sequence of data that you are about to read. Thow, I would say that probably the solution behind the SharePoint API provides a common stream of data that you read from the server.

0
source

It can be assumed that if ICollection had an Add method, what would it take? Object , of course. This was a big problem with the lack of a shared array that was before C # 2.0.

The problem is that you can add another type to the same collection

0
source

Wild guess - the ICollection interface is the base interface for other interfaces that extend it, for example, IList and IDictionary. These interfaces have different implementations of the add method. IList accepts one parameter, IDictionary needs two explicitly. With generics, the signatures of the derived interface method are not really different from each other, because they take one parameter - the type.

0
source

There were no common interfaces when creating ICollection . This meant that if there was an Add method on ICollection , it would have to have the Add(object) signature. ICollection designed to declare a consistent interface between collections of any type - which will make each collection act, in part, as a collection of object s.

This has been fixed in ICollection<T> , which has an Add(T) method.

0
source

According to Albahari Brothers

General and non-general versions differ in ways that exceed what you can expect, especially in the case of ICollection.

The reasons for this are mostly historical: because generalized later appeared , common interfaces were designed with benefit in hindsight .

For this reason

ICollection<T> does not extend ICollection ,

IList<T> does not extend IList ,

and IDictionary<TKey, TValue> not covered by IDictionary .

To summarize, ICollection<T> evolved to prevent errors made in ICollection . Therefore, ICollection<T> has an Add method and ICollection not ..

0
source

All Articles