简体   繁体   English

IEquatable 和 inheritance

[英]IEquatable and inheritance

how would i make one parent and one child and inherit the IEquatable function for the child?我将如何让一个父母和一个孩子继承孩子的 IEquatable function? here is my code这是我的代码

public class Category : IEquatable<Category>
{
   public string _Name { get; set; }
   public string _Id { get; set;}

   private string _HomeCategory { get; set; }  
   public string _prestaId { get; set; }

   public bool _inUse { get; set; }

   public Category(string Name, string Id, string HomeCategory, bool inUse)
    {
        _Name = Name;
        _Id = Id;
        _HomeCategory = HomeCategory;
        _inUse = inUse;
    }

    public virtual bool Equals(Category other)
    {
        if (other == null)
            return false;

        if(this._Id == other._Id)
        {
            return true;
        }
        else { return false; }
    }

    public override int GetHashCode()
    {
        return this._Id.GetHashCode();
    }
}

public class SubCategory : Category :   
{
    public string parentCategory { get; set; }

    public SubCategory(string Name, string Id, string HomeCategory, bool inUse) : base(Name, Id, HomeCategory, inUse)
    {

    }
}

so i have to object and subcategory derives from category but contain´s method does not work with subcategory but it does with category what am i doing wrong how would i derive the method of Category so that contains on a list with subcategory as its type would work?所以我必须 object 和子类别派生自类别,但包含的方法不适用于子类别,但它适用于类别我做错了什么我将如何派生类别的方法,以便包含在具有子类别的列表中工作?

It seems that you need to implement IEquatable<SubCategory> on your SubCategory .看来您需要在您的 SubCategory 上实现IEquatable<SubCategory> SubCategory And SubCategory definition should change to public class SubCategory:Category, IEquatable<SubCategory> .并且SubCategory定义应更改为public class SubCategory:Category, IEquatable<SubCategory>

Without this you will have next behaviour:没有这个,你将有下一个行为:

var sub = new SubCategory { _Name = "1", parentCategory = "2" };
var x = new List<Category>();
x.Add(sub);
Console.WriteLine(x.Contains(new SubCategory
{
    _Name = sub._Name,
    parentCategory = sub.parentCategory
})); // prints "True"

var y = new List<SubCategory>();
y.Add(sub);
Console.WriteLine(y.Contains(new SubCategory
{
    _Name = sub._Name,
    parentCategory = sub.parentCategory
})); // prints "False"

And it makes sense, cause SubCategory is IEquatable<Category> but is not IEquatable<SubCategory> , and IEnumerable.Contains docs state:这是有道理的,因为SubCategoryIEquatable<Category>但不是IEquatable<SubCategory> ,并且IEnumerable.Contains 文档state:

Elements are compared to the specified value by using the default equality comparer, Default.使用默认相等比较器 Default 将元素与指定值进行比较。

Which internally uses the implementation of the IEquatable<T>.Equals in your class.它在内部使用 class 中的IEquatable<T>.Equals的实现。

SubCategory does inherit bool Equals(Category other) , but it would apply to any Category instance- meaning that a SubCategory and a Category would be "equal" if they have the same Id . SubCategory确实继承了bool Equals(Category other) ,但它适用于任何Category实例——这意味着如果SubCategoryCategory具有相同的Id ,它们将是“相等的”。

It sounds like you want an "automatic" implementation of IEquatable<SubCategory> and a bool Equals(SubCategory other) implementation, which is not possible.听起来您想要IEquatable<SubCategory>的“自动”实现和bool Equals(SubCategory other)实现,这是不可能的。 You'd need to explicitly add the interface declaration, but the implementation should be trivial:您需要显式添加接口声明,但实现应该是微不足道的:

public class SubCategory : Category , IEquatable<SubCategory>  
{
    public string parentCategory { get; set; }

    public SubCategory(string Name, string Id, string HomeCategory, bool inUse) : base(Name, Id, HomeCategory, inUse)
    {

    } 

    public bool Equals(SubCategory other)
    {
        return ((Category).this).Equals((Category)other)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM