本文介绍了如何检查类对象中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
需要检查对象属性中是否有重复项。
ex:
need to check if any duplicates in an object properties.
ex:
class Emp{
public string id;
public string Ename;
public string Eadd;
SVM()
{
Emp emp = new Emp(){id=1,Ename="raja",Eadd="1"};
}
现在id和eadd包含重复项,
如何检查重复项。
}
请求:请尝试理解,而不是拒绝任何其他无用的评论。
now the id and eadd contains duplicates,
how to check duplicates.
}
request:please try to understand ,instead reject any other un useful comments.
推荐答案
public bool hasDuplicates()
{
bool has_dup = false;
if ( id == Ename || id == Eadd || Eadd == Ename) has_dup = true;
return has_dup;
}
class Emp
{
public string id;
public string Ename;
public string Eadd;
public bool HasDuplicates
{
get
{
if (id == Ename || id == Eadd)
{
return true;
}
if (Ename == Eadd)
{
return true;
}
return false;
}
}
}
使用
usage
Emp e1 = new Emp { id = "1", Ename = "raja", Eadd = "1" };
Console.WriteLine(e1.HasDuplicates);
Emp e2 = new Emp { id = "2", Ename = "raja", Eadd = "3" };
Console.WriteLine(e2.HasDuplicates);
Emp e3 = new Emp { id = "3", Ename = "raja", Eadd = "raja" };
Console.WriteLine(e3.HasDuplicates);
这篇关于如何检查类对象中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!