The advantage with IList
is there are many different classes that can be assigned to it. This can be handy if you're sourcing data from different places that use different classes.
IList values;
values = new ArrayList();
values = new object[] {};
values = new List<int>();
values = new DataView();
However, if you use an IList
, you can only use the methods defined by an IList
. If you define the variable as an ArrayList
or any other concrete class, you have access to all of that class's methods.
ArrayList values = new ArrayList();
values.
Using the var
keyword will tell the compiler to use the same class as the result of the expression. It can be very useful if you have a very long-winded class.
var values = new ArrayList();
// values is ArrayList
// Assuming a function: List<int> GetIntegerList() { ... }
var values = GetIntegerList();
// values is List<int>
// Assuming a function: Dictionary<string, Dictionary<string, List<Tuple<int, int, string>>>> GetSettingsCollection() { ... }
var values = GetSettingsCollection();
// values is Dictionary<string, Dictionary<string, List<Tuple<int, int, string>>>>