Definition of List
in .net shows it implement various interfaces.
public class List : IList, ICollection, I
The reason that List<T>
implements both IList<T>
and IList
is to make it usable anywhere your code is assuming an IList
. This will make it more easier to make a transition to the generic IList<T>
since it is the more appropriate one. Also, if you have a .net 1.1 or earlier code that you would like to reuse even though your class is implemented in a .net 2.0 or later assembly it will make it possible.
In a IList<T>
you can only put the defined(T) type of object in and a IList can contain diffrent types of objects
This code below will not complie because of the AdressInformation is not an valid object in customers List
IList<Customer> customers = new List<Customer>();
customers.Add(new Customer());
customers.Add(new AdressInformation());
This code will compile but cast an exception in runtime
IList customers = new List<Customer>();
customers.Add(new Customer());
customers.Add(new AdressInformation());
Question:
What is the difference between IList
and IList<T>
?
Answer:
Both IList
and IList<T>
are interfaces, but IList
is not generic and it is both member of and belongs to the System.Collections
namespace while IList<T>
is generic and it is both member of and belongs to the System.Collections.Generic
namespace.
Also System.Collections.IList
is simply both equivalent and identical to System.Collections.Generic.IList<object>
as well.
Since object
, in C#, can be anything and every type that can be used in the generic type T
, inside the angle brackets, extends object
, this means that System.Collections.IList
and System.Collections.Generic.IList<T>
have the same expressive power.
This also says that all the tasks that can be done with the help of System.Collections.IList
interface, these tasks can also be done with the help of System.Collections.Generic.IList<T>
interface.
If so then what to prefer: System.Collections.IList
or System.Collections.Generic.IList<T>
?
While System.Collections.IList
is shorter and requires less typing, System.Collections.Generic.IList<T>
is still much more both better and recommended, because it makes your code all more precise, accurate, exact, correct, strict, informative, explanatory, detailed, elaborate, itemized, particularized, expressive, clear, readable and legible and if the programmer makes any mistake in using the generic interface, probably that compilation will fail and the programmer will immediately know and understand what he/she did wrong, correct the errors and the customer may encounter less bugs in the application.
But using the non generic interface improperly may create unknown bugs that throw cast failure exceptions in the application that the customer may find, make him/her unsatisfied, report to the developers about these bugs and may also demand/require/initiate a refund.
For an example:
System.Collections.Generic.IList<int> list = GetSomeListOfIntegers();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add("four");
list.Add(5);
The above code doesn't compile because of the line list.Add("four");
.
The error says, more or less, that the argument must be int
, but it was string
.
This is good to see errors of compiler and correct all of them before building the application. This way, the customer won't encounter unnecessary bugs in the application and remain satisfied.
But if the programmer does not change the line list.Add("four");
, that causes the compiler to fail, but instead replaces System.Collections.Generic.IList<int>
by System.Collections.IList
and modifies the method GetSomeListOfIntegers
to return an instance of System.Collections.IList
instead of an instance of System.Collections.Generic.IList<int>
Then the code compiles successfully, but when list.Add("four");
is executed, a cast failure exception is thrown and then the application stops executing and terminates.
And who knows which customer will find this bug if all the programmers and developers will fail to find this bug.
This is the reason why all the generic classes and interfaces, including IList<T>
, of the namespace System.Collections.Generic
are much better than all the classes and interfaces, including IList
, of the namespace System.Collections
and System.Collections.Generic
should be the recommended namespace in general.
Historically the namespace System.Collections
and all the classes and interfaces defined in this namespace, including the interface IList
, were exist before the namespace System.Collections.Generic
and all it's classes and interfaces including the IList<T>
generic interface.
The namespace System.Collections.Generic
and all it's classes and interfaces were introduced in a later version of the .NET Framework of Microsoft.