list.contains 问题改进

li.Add(new VisaDatum{ typeName= "abc", imgeurl= "123" });
li.Add(new VisaDatum{ typeName= "123", imgeurl= "abc" });

Users u = new Users();
u.typeName= li[0].typeName;
u.imgeurl=li[0].imgeurl;

if (li.Contains(u)) //怎么判断都是false
{

}

解决方案:

  public class VisaDatum
    {
        public string typeName{get;set;}
        public string imgeurl{get;set;}
        public override bool Equals(object obj)
        {
            if (typeName == (obj as VisaDatum).typeName)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }




你可能感兴趣的:(.net知识)