I have a business object class BusinessObject which implements an interface IAlternateInterface. I already have a method that will return a generic list of BusinessObject which has the objects I want but I want to get them as a list of IAlternateInterface. I tried to do something like the following psudo code but I am getting a “Can not convert source type … to target type …” message in Visual Studio. What is the best way to convert the list?
public List<BusinessObject> GetObjects(){
//logic to get the list
}
public List<IAlternateInterface> GetInterfaceObjects(){
return GetObjects();
}
You’re looking for Enumerable.Cast<TResult>:
The ToList at the end is only necessary if you need a list. Cast<TResult> returns an IEnumerable<TResult>