Thursday 15 February 2007

C# Generics typecasting

Error being received:

cannot implicity convert type ilist to list

The error occurs when calling a function that returns an IList.
e.g.

IList myFunction();
....
List myVar = new List;
myVar=myFunction();


the error occurs on the assignment of myVar=myFunction.
The local var myVar should be declared as IList.
In the above instance the new List is not required because myFunction creates the instance.
Correct code:

IList myFunction();
....
IList myVar;
myVar=myFunction();