如何检查类对象中的重复项 [英] how to check duplicates in class object

查看:76
本文介绍了如何检查类对象中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要检查对象属性中是否有重复项。

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.

推荐答案

这很复杂,因为您需要针对所有其他现有实例进行检查 - 并且因为每个实例都独立于所有其他实例,这意味着您需要保留静态集合并在类构造函数中添加它。然后,您可以使用该静态集合来确定新实例是否为新。

但这并不像听起来那么简单,因为没有自动的方法来判断实例何时被删除 - 你不能使用自动Dispose,因为垃圾收集器永远不会将任何实例作为它将永远是静态集合的一部分!所以你必须确保你的消费者总是明确地自己处理实例,或者在完成它时调用一个方法将它从静态集合中删除。

说实话,它通常更多很难做到这样的事情,而不是让用户实际检查他们的特定集合是否已引用相同的值。这是微不足道的:它只是一个Linq Where语句!
That's complicated, because you would need to check it against all other existing instances - and because each instance is independent of all others, that means you need to keep a static collection and add to it in the class constructor. You can then use that static collection to determine if the new instance is "new" or not.
But that's not as simple as it sounds, because there is no automatic way to tell when an instance has been deleted - you can't use the automatic Dispose as the Garbage Collector will never Dispose any instance as it will always be part of the static collection! So you have to make sure that your consumers always explicitly either Dispose the instance themselves, or call a method to remove it from the static collection when they are finished with it.
To be honest, it's generally more hassle to do something like this, than to make you users actually check if their specific collection is already referencing an identical value. That's trivial: it's just a Linq Where statement!


如果你真的需要检查任何一对对象属性是否保持相同的值,那么写一个方法来检查这样的条件,例如

If you really need to check if any pair of the object properties hold the same value then write a method to check for such a condition, e.g.
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);


这篇关于如何检查类对象中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆