So I have a basic class that inherits an interface as below
public interface IRefDataItem
{
int RefID { get; set; }
string Code { get; set; }
}
public class RACodeItem : IRefDataItem
{
public int RefID { get; set; }
public string Code { get; set; }
}
I then have a method that return a List of RACodeItem which I am attempting to assign to a List of IRefDataItem as below
List<IRefDataItem> codes = GetRACodes( ); //GetRACodes returns List of RACodeItem
I’m getting an error here stating that cannot implicitly cast type List of RACodeItem to List of IRefDataItem, despite the fact that RACodeItem inherits IRefDataItem
Am I missing something simple here? How can I cast a List of T to a List of an interface type that T implements?
Cheers
Stewart
I understand why you are trying this but if you think about you will realise this can’t work.
For example (This code won’t compile)
The problem is you can’t add Test2 to a List of Test1. Declaring you list as
List<ITest> list = new List<ITest>();will compile and you can add both Test1 and Test2.