比较通用枚举

时间:2018-01-24 15:28:04

标签: c# generics enums comparison

尝试比较通用枚举时出现编译器错误。一切看起来很好,我不知道什么是错的。

错误讯息:Operator == cannot be applied to operands of type T and T

public class SpriteLink<T> 
{
    public T id;
    public Sprite sprite;

    public SpriteLink(T id, Sprite sprite)
    {
        this.id = id;
        this.sprite = sprite;
    }
}

public class LinkList<T>
{
    public List<SpriteLink<T>> links = new List<SpriteLink<T>>();

    public bool Contains(T id)
    {
        links[0].id = id; // compiler passes this line without any errors
        return links.Any(link => link.id == id); // but throws compiler error here (why?)
    }

    public bool Set(T id, Sprite sprite)
    {
        if (!Contains(id))
        {
            links.Add(new SpriteLink<T>(id, sprite));
            return true;
        }
        return false;
    }
}

public LinkList<SurfaceID> surfaceLinks = new LinkList<SurfaceID>();
public LinkList<FloorID> floorLinks = new LinkList<FloorID>();

SurfaceIDFloorID只是包含某些字段的枚举。

0 个答案:

没有答案