我收到一個錯誤。這裏是整個複製到一個控制檯項目和精簡代碼:實施IEnumerable <T>列表<T>衍生集合
namespace ConsoleApplication1
{
public interface IHexGrid
{
IEnumerable<Hex> hexs { get; } //error related location
}
public class HexC : Hex
{ public int var1;}
public abstract class Hex
{ public int var2; }
public class HexGridC : IHexGrid //error CS0738
{
public List<HexC> hexs { get; set; } // error related location
}
class Program
{
static void Main(string[] args)
{
}
}
}
,我發現了以下內容:錯誤CS0738:
'ConsoleApplication1.HexGridC' does not implement interface
member 'ConsoleApplication1.IHexGrid.hexs'. 'ConsoleApplication1.HexGridC.hexs' cannot
implement 'ConsoleApplication1.IHexGrid.hexs' because it does not have the matching
return type of '`System.Collections.Generic.IEnumerable<ConsoleApplication1.Hex>`'.
不知道爲什麼爲IEnumerable是協變。任何幫助非常感謝。
編輯:代碼已被簡化
老實說,編譯錯誤信息不能比這更好...它告訴你到底該怎麼做':)' – Kobi