C#的true运算符和false运算符重载

 

从微软官网找到的例子。

true false重载   
 // dbbool.cs  
 using System;  
   
 public struct DBBool  
 {  
     // The three possible DBBool values:  
     public static readonly DBBool dbNull = new DBBool(0);  
     public static readonly DBBool dbFalse = new DBBool(-1);  
     public static readonly DBBool dbTrue = new DBBool(1);  
     // Private field that stores -1, 0, 1 for dbFalse, dbNull, dbTrue:  
     int value;  
   
     // Private constructor. The value parameter must be -1, 0, or 1:  
     DBBool(int value)  
     {  
         this.value = value;  
     }  
   
     // Implicit conversion from bool to DBBool. Maps true to   
     // DBBool.dbTrue and false to DBBool.dbFalse:  
     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;  
     }  
   
     // Inequality 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;  
     }  
   
     // Logical negation operator. Returns dbTrue if the operand is   
     // dbFalse, dbNull if the operand is dbNull, or dbFalse if the  
     // operand is dbTrue:  
     public static DBBool operator !(DBBool x)  
     {  
         return new DBBool(-x.value);  
     }  
   
     // Logical AND operator. Returns dbFalse if either operand is   
     // dbFalse, dbNull if either operand is dbNull, otherwise dbTrue:  
     public static DBBool operator &(DBBool x, DBBool y)  
     {  
         return new DBBool(x.value < y.value ? x.value : y.value);  
     }  
   
     // Logical OR operator. Returns dbTrue if either operand is   
     // dbTrue, dbNull if either operand is dbNull, otherwise dbFalse:  
     public static DBBool operator |(DBBool x, DBBool y)  
     {  
         return new DBBool(x.value > y.value ? x.value : y.value);  
     }  
   
     // Definitely true operator. Returns true if the operand is   
     // dbTrue, false otherwise:  
     public static bool operator true(DBBool x)  
     {  
         return x.value > 0;  
     }  
   
     // Definitely false operator. Returns true if the operand is   
     // dbFalse, false otherwise:  
     public static bool operator false(DBBool x)  
     {  
         return x.value < 0;  
     }  
   
     // Overload the conversion from DBBool to string:  
     public static implicit operator string(DBBool x)  
     {  
         return x.value > 0 ? "dbTrue" 
              : x.value < 0 ? "dbFalse" 
              : "dbNull";  
     }  
   
     // Override the Object.Equals(object o) method:  
     public override bool Equals(object o)  
     {  
         try 
         {  
             return (bool)(this == (DBBool)o);  
         }  
         catch 
         {  
             return false;  
         }  
     }  
   
     // Override the Object.GetHashCode() method:  
     public override int GetHashCode()  
     {  
         return value;  
     }  
   
     // Override the ToString method to convert DBBool to a string:  
     public override string ToString()  
     {  
         switch (value)  
         {  
             case -1:  
                 return "DBBool.False";  
             case 0:  
                 return "DBBool.Null";  
             case 1:  
                 return "DBBool.True";  
             default:  
                 throw new InvalidOperationException();  
         }  
     }  
 }  
   
 class Test  
 {  
     static void Main()  
     {  
         DBBool a, b;  
         a = DBBool.dbTrue;  
         b = DBBool.dbNull;  
   
         Console.WriteLine("!{0} = {1}", a, !a);  
         Console.WriteLine("!{0} = {1}", b, !b);  
         Console.WriteLine("{0} & {1} = {2}", a, b, a & b);  
         Console.WriteLine("{0} | {1} = {2}", a, b, a | b);  
         // Invoke the true operator to determine the Boolean   
         // value of the DBBool variable:  
         if (b)  
             Console.WriteLine("b is definitely true");  
         else 
             Console.WriteLine("b is not definitely true");  
     }  
 } 
其中,在if(b)的时候,调用了public static bool operator true(DBBool x)
很明显,这个重载的用处是可以把这个类型当做真假值来用,和bool型一样了。


 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值