I am trying to return IEnumerable<IMyInterface>. I have a class, MyClass:IMyInterface that I return from a function.
IEnumerable<IMyInterface> test() {
tmpList = new List<MyClass>();
tmp1 = new MyClass();
tmp2 = new MyClass();
tmpList.Add(tmp1);
tmpList.Add(tmp2);
return tmpList;
}
Compiler will not permit, which seems odd to me since MyClass:MyInterface. Compiler gives error along the lines of 'cannot implicitly convert type System.Collections.Generic.IEnumerable<MyClass> to System.Collections.Generic.IEnumerable<IMyInterface. An explicit conversion exists. Are you missing a cast?'
I can’t perform return (IEnumerable<IMyInterface>)tmp without cast exception at runtime. What am I missing? I would expect returning IEnumerable of interface should work fine.
You should do this:
Alternatively, you could do this: