true运算符和false运算符重载的例子

从微软官网找到的例子。

true false重载
  1 // dbbool.cs
  2 using System;
  3 
  4 public struct DBBool
  5 {
  6     // The three possible DBBool values:
  7     public static readonly DBBool dbNull = new DBBool(0);
  8     public static readonly DBBool dbFalse = new DBBool(-1);
  9     public static readonly DBBool dbTrue = new DBBool(1);
 10     // Private field that stores -1, 0, 1 for dbFalse, dbNull, dbTrue:
 11     int value;
 12 
 13     // Private constructor. The value parameter must be -1, 0, or 1:
 14     DBBool(int value)
 15     {
 16         this.value = value;
 17     }
 18 
 19     // Implicit conversion from bool to DBBool. Maps true to 
 20     // DBBool.dbTrue and false to DBBool.dbFalse:
 21     public static implicit operator DBBool(bool x)
 22     {
 23         return x ? dbTrue : dbFalse;
 24     }
 25 
 26     // Explicit conversion from DBBool to bool. Throws an 
 27     // exception if the given DBBool is dbNull, otherwise returns
 28     // true or false:
 29     public static explicit operator bool(DBBool x)
 30     {
 31         if (x.value == 0) throw new InvalidOperationException();
 32         return x.value > 0;
 33     }
 34 
 35     // Equality operator. Returns dbNull if either operand is dbNull, 
 36     // otherwise returns dbTrue or dbFalse:
 37     public static DBBool operator ==(DBBool x, DBBool y)
 38     {
 39         if (x.value == 0 || y.value == 0) return dbNull;
 40         return x.value == y.value ? dbTrue : dbFalse;
 41     }
 42 
 43     // Inequality operator. Returns dbNull if either operand is
 44     // dbNull, otherwise returns dbTrue or dbFalse:
 45     public static DBBool operator !=(DBBool x, DBBool y)
 46     {
 47         if (x.value == 0 || y.value == 0) return dbNull;
 48         return x.value != y.value ? dbTrue : dbFalse;
 49     }
 50 
 51     // Logical negation operator. Returns dbTrue if the operand is 
 52     // dbFalse, dbNull if the operand is dbNull, or dbFalse if the
 53     // operand is dbTrue:
 54     public static DBBool operator !(DBBool x)
 55     {
 56         return new DBBool(-x.value);
 57     }
 58 
 59     // Logical AND operator. Returns dbFalse if either operand is 
 60     // dbFalse, dbNull if either operand is dbNull, otherwise dbTrue:
 61     public static DBBool operator &(DBBool x, DBBool y)
 62     {
 63         return new DBBool(x.value < y.value ? x.value : y.value);
 64     }
 65 
 66     // Logical OR operator. Returns dbTrue if either operand is 
 67     // dbTrue, dbNull if either operand is dbNull, otherwise dbFalse:
 68     public static DBBool operator |(DBBool x, DBBool y)
 69     {
 70         return new DBBool(x.value > y.value ? x.value : y.value);
 71     }
 72 
 73     // Definitely true operator. Returns true if the operand is 
 74     // dbTrue, false otherwise:
 75     public static bool operator true(DBBool x)
 76     {
 77         return x.value > 0;
 78     }
 79 
 80     // Definitely false operator. Returns true if the operand is 
 81     // dbFalse, false otherwise:
 82     public static bool operator false(DBBool x)
 83     {
 84         return x.value < 0;
 85     }
 86 
 87     // Overload the conversion from DBBool to string:
 88     public static implicit operator string(DBBool x)
 89     {
 90         return x.value > 0 ? "dbTrue"
 91              : x.value < 0 ? "dbFalse"
 92              : "dbNull";
 93     }
 94 
 95     // Override the Object.Equals(object o) method:
 96     public override bool Equals(object o)
 97     {
 98         try
 99         {
100             return (bool)(this == (DBBool)o);
101         }
102         catch
103         {
104             return false;
105         }
106     }
107 
108     // Override the Object.GetHashCode() method:
109     public override int GetHashCode()
110     {
111         return value;
112     }
113 
114     // Override the ToString method to convert DBBool to a string:
115     public override string ToString()
116     {
117         switch (value)
118         {
119             case -1:
120                 return "DBBool.False";
121             case 0:
122                 return "DBBool.Null";
123             case 1:
124                 return "DBBool.True";
125             default:
126                 throw new InvalidOperationException();
127         }
128     }
129 }
130 
131 class Test
132 {
133     static void Main()
134     {
135         DBBool a, b;
136         a = DBBool.dbTrue;
137         b = DBBool.dbNull;
138 
139         Console.WriteLine("!{0} = {1}", a, !a);
140         Console.WriteLine("!{0} = {1}", b, !b);
141         Console.WriteLine("{0} & {1} = {2}", a, b, a & b);
142         Console.WriteLine("{0} | {1} = {2}", a, b, a | b);
143         // Invoke the true operator to determine the Boolean 
144         // value of the DBBool variable:
145         if (b)
146             Console.WriteLine("b is definitely true");
147         else
148             Console.WriteLine("b is not definitely true");
149     }
150 }

其中,在if(b)的时候,调用了public static bool operator true(DBBool x)

很明显,这个重载的用处是可以把这个类型当做真假值来用,和bool型一样了。

 

posted @ 2013-03-16 08:50  BIT祝威  阅读(1027)  评论(0编辑  收藏  举报
canvas start.

canvas end.