Definition of List
in .net shows it implement various interfaces.
public class List : IList, ICollection, I
Question:
What is the difference between IList
and IList
?
Answer:
Both IList
and IList
are interfaces, but IList
is not generic and it is both member of and belongs to the System.Collections
namespace while IList
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
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
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
interface.
If so then what to prefer: System.Collections.IList
or System.Collections.Generic.IList
?
While System.Collections.IList
is shorter and requires less typing, System.Collections.Generic.IList
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 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
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
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
, 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
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.