C# 与C中的对象进行比较#
你好,我是C#的新手,我有一个小问题: 当我调用方法C# 与C中的对象进行比较#,c#,object,compare,C#,Object,Compare,你好,我是C#的新手,我有一个小问题: 当我调用方法list1.contains(product)时,我们需要重写Equals方法吗 如果有的话?这样做吗 public partial class product { public product() { } public int idProduct { get; set; } public override bool Equals(object obj) { if (this
list1.contains(product)
时,我们需要重写Equals方法吗
如果有的话?这样做吗
public partial class product
{
public product()
{
}
public int idProduct { get; set; }
public override bool Equals(object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (this.GetType() != obj.GetType())
{
return false;
}
product other = (product)obj;
if (idProduct != other.idProduct)
{
return false;
}
return true;
}
}
是的,您应该重写Equals以为类提供相等语义。 默认情况下,引用相等将起作用 您的Equals方法将起作用,但在C#中,我们也可以重写==运算符。因此,在Equals方法中使用它可能不安全。最好使用
ReferenceEquals
进行参考比较:
public override bool Equals(object obj)
{
if (ReferenceEquals(obj,null))
{
return false;
}
if (ReferenceEquals(this,obj))
{
return true;
}
if (this.GetType() != obj.GetType())
{
return false;
}
product other = (product)obj;
if (idProduct != other.idProduct)
{
return false;
}
return true;
}
是的,您应该重写Equals以为类提供相等语义。 默认情况下,引用相等将起作用 您的Equals方法将起作用,但在C#中,我们也可以重写==运算符。因此,在Equals方法中使用它可能不安全。最好使用
ReferenceEquals
进行参考比较:
public override bool Equals(object obj)
{
if (ReferenceEquals(obj,null))
{
return false;
}
if (ReferenceEquals(this,obj))
{
return true;
}
if (this.GetType() != obj.GetType())
{
return false;
}
product other = (product)obj;
if (idProduct != other.idProduct)
{
return false;
}
return true;
}
可能值得一提的是,通过实现
IEquatable
,您可以稍微缩短代码
如果泛型列表中的类型实现了IEquatable
,则将调用IEquatable.Equals
,以避免强制转换:
public class product : IEquatable<product>
{
public int idProduct { get; set; }
// List<product>.Contains will call IEquatable<product>.Equals if available.
public bool Equals(product other)
{
return ReferenceEquals(other, this) || other.idProduct == this.idProduct;
}
// Note: you should still override object.Equals.
public override bool Equals(object other)
{
return this.Equals(other as product);
}
}
公共类产品:IEquatable
{
public int idProduct{get;set;}
//List.Contains将调用IEquatable.Equals(如果可用)。
公共布尔等于(产品其他)
{
返回ReferenceEquals(other,this)| other.idProduct==this.idProduct;
}
//注意:仍应覆盖object.Equals。
公共覆盖布尔等于(对象其他)
{
返回此。等于(其他为产品);
}
}
同样在C中,类通常是
PascalCased
而不是camelCased
值得一提的是,通过实现IEquatable
,您可以稍微缩短代码
如果泛型列表中的类型实现了IEquatable
,则将调用IEquatable.Equals
,以避免强制转换:
public class product : IEquatable<product>
{
public int idProduct { get; set; }
// List<product>.Contains will call IEquatable<product>.Equals if available.
public bool Equals(product other)
{
return ReferenceEquals(other, this) || other.idProduct == this.idProduct;
}
// Note: you should still override object.Equals.
public override bool Equals(object other)
{
return this.Equals(other as product);
}
}
公共类产品:IEquatable
{
public int idProduct{get;set;}
//List.Contains将调用IEquatable.Equals(如果可用)。
公共布尔等于(产品其他)
{
返回ReferenceEquals(other,this)| other.idProduct==this.idProduct;
}
//注意:仍应覆盖object.Equals。
公共覆盖布尔等于(对象其他)
{
返回此。等于(其他为产品);
}
}
同样在C中,类通常是PascalCased
而不是camelCased
此外,您还可以创建一个新的类:
其中List 1
是List
定义为:
Product product = new Product { idProduct = 1 };
List<Product> list1 = new List<Product>
{
new Product{idProduct = 1},
new Product{idProduct = 2}
};
除此之外,您还可以创建新的,如:
其中List 1
是List
定义为:
Product product = new Product { idProduct = 1 };
List<Product> list1 = new List<Product>
{
new Product{idProduct = 1},
new Product{idProduct = 2}
};
+1.值得一提的是,大多数Linq/collection方法都将比较器作为参数-因此严格来说,不必实现自定义
Equal
@AlexeiLevenkov您的意思是什么?@HamletHakobyan.+1。值得一提的是,大多数Linq/collection方法都将比较器作为参数,因此不必严格地实现自定义Equal
@AlexeiLevenkov你的意思是什么?@HamletHakobyan。