日期:2014-05-17  浏览次数:20857 次

c# 类相等的问题
类定义如下:
C# code
    public class CDnInfo : IEquatable<CDnInfo >
    {
        public int iRank;
        public string strDn;
        public string strKeyword;
        public string strOwner;
        public string strIncrease;
        public string strDate;
        public string strFlag;
        
        public static int getMemCount()
        {
            return 10;
        }
        
        public bool Equals(CDnInfo y)
        {
            if (this == null || y == null) return false;
            return this.strDn== y.strDn;
        }
        
    }

使用的时候:
List<CDnInfo> list=.................
CDnInfo dninfo=.................
判断
if(list.contains(dninfo))
这样子是可以的,能够判断出相等或者不等。
但是如果使用
dnInfo1==dnInfo2 
则判断不出来
怎么办啊
如何更改CDnInfo,能够使得dnInfo1==dnInfo2这种形式也能判断相等

------解决方案--------------------
public static DBBool operator ==(DBBool x, DBBool y) 
{
if (x.value == 0 || y.value == 0) return dbNull;
return x.value == y.value? dbTrue: dbFalse;
}