读书人

关于满载运算符的疑问

发布时间: 2013-02-19 11:11:40 作者: rapoo

关于重载运算符的疑问
本帖最后由 huanhuan30 于 2013-02-17 14:19:54 编辑 《professional c# 4.0 and .net 4》上说
“In addition, the comparison operators must return a bool”

但是我在
http://msdn.microsoft.com/en-us/library/aa288467(v=VS.71).aspx
example2 上看到他返回的是自己的 类



我到底该怎么理解?
[解决办法]

   public static implicit operator DBBool(bool x) 
{
return x? dbTrue: dbFalse;
}

// Explicit conversion from DBBool to bool. Throws an
// exception if the given DBBool is dbNull, otherwise returns
// true or false:
public static explicit operator bool(DBBool x)
{
if (x.value == 0) throw new InvalidOperationException();
return x.value > 0;
}

// Equality operator. Returns dbNull if either operand is dbNull,
// otherwise returns dbTrue or dbFalse:
public static DBBool operator ==(DBBool x, DBBool y)
{
if (x.value == 0
[解决办法]
y.value == 0) return dbNull;
return x.value == y.value? dbTrue: dbFalse;
}

人家有隐式转换

读书人网 >C#

热点推荐