gpt4 book ai didi

c# - 覆盖 Equals() 和 GetHashCode() 的正确方法

转载 作者:IT王子 更新时间:2023-10-29 03:37:10 26 4
gpt4 key购买 nike

<分区>

我以前从未真正做过这件事,所以我希望有人能告诉我为我的类(class)实现 Except() 和 GetHashCode() 覆盖的正确方法。

我正在尝试修改该类,以便我可以使用 LINQ Except() 方法。

public class RecommendationDTO{public Guid RecommendationId { get; set; }
public Guid ProfileId { get; set; }
public Guid ReferenceId { get; set; }
public int TypeId { get; set; }
public IList<TagDTO> Tags { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime? ModifiedOn { get; set; }
public bool IsActive { get; set; }
public object ReferencedObject { get; set; }
public bool IsSystemRecommendation { get; set; }
public int VisibilityScore { get; set; }

public RecommendationDTO()
{
}

public RecommendationDTO(Guid recommendationid,
Guid profileid,
Guid referenceid,
int typeid,
IList<TagDTO> tags,
DateTime createdon,
DateTime modifiedon,
bool isactive,
object referencedobject)
{
RecommendationId = recommendationid;
ProfileId = profileid;
ReferenceId = referenceid;
TypeId = typeid;
Tags = tags;
CreatedOn = createdon;
ModifiedOn = modifiedon;
ReferencedObject = referencedobject;
IsActive = isactive;
}

public override bool Equals(System.Object obj)
{
// If parameter is null return false.
if (obj == null)
{
return false;
}

// If parameter cannot be cast to Point return false.
RecommendationDTO p = obj as RecommendationDTO;
if ((System.Object)p == null)
{
return false;
}

// Return true if the fields match:
return (ReferenceId == p.ReferenceId);// && (y == p.y);
}

public bool Equals(RecommendationDTO p)
{
// If parameter is null return false:
if ((object)p == null)
{
return false;
}

// Return true if the fields match:
return (ReferenceId == p.ReferenceId);// && (y == p.y);
}

//public override int GetHashCode()
//{
// return ReferenceId;// ^ y;
//}}

我看过http://msdn.microsoft.com/en-us/library/ms173147.aspx但我希望有人能以我自己的例子向我展示。

如有任何帮助,我们将不胜感激。

谢谢

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com