5 // Derek Holden (dholden@draper.com)
7 // (C) Ximian, Inc. http://www.ximian.com
11 // I guess this is the Boolean class. This was written word for word
12 // off the Library specification for System.Boolean in the ECMA
13 // TC39 TG2 and TG3 working documents.
15 // The XML style documentation isn't that elegant, but it seems to
16 // be the standard way according to the poorly documented C#
17 // Programmer's Reference section on XML Documentation.
19 // This header and the one above it can be formatted however, just trying
20 // to keep it consistent w/ the existing mcs headers.
22 // Even though it's not in the ECMA docs, the .NET Framework Class Library
23 // says this implements IConvertible, but if it does it has some other
24 // member functions to implement.
27 using System.Globalization;
31 /// Represents the boolean values of logical true and false.
34 public struct Boolean : IComparable, IConvertible {
37 /// The String representation of Boolean False
39 public static readonly string FalseString;
42 /// The String representation of Boolean True
44 public static readonly string TrueString;
47 /// Internal bool value for for this instance
54 FalseString = "False";
59 /// Compares the current Boolean instance against another object.
62 /// Throws an ArgumentException if <c>obj</c> isn't null or
65 /// <param name="obj">
66 /// The object to compare against
69 /// An int reflecting the sort order of this instance as
70 /// compared to <c>obj</c>
71 /// -1 if this instance is false and <c>obj</c> is true
72 /// 0 if this instance is equal to <c>obj</c>
73 /// 1 if this instance is true and <c>obj</c> is false,
74 /// or <c>obj</c> is null
76 public int CompareTo (object obj)
81 if (!(obj is System.Boolean))
82 throw new ArgumentException
83 (Locale.GetText ("Object is not a Boolean and is not a null reference"));
86 if (obj == null || (value == true && (bool)obj == false))
89 // for case #2, else it's #1
90 return (value == (bool)obj) ? 0 : -1;
94 /// Determines whether this instance and another object represent the
95 /// same type and value.
97 /// <param name="obj">
98 /// The object to check against
101 /// true if this instnace and <c>obj</c> are same value,
102 /// otherwise false if it is not or null
104 public override bool Equals (Object obj)
106 if (obj == null || !(obj is System.Boolean))
109 return ((bool)obj) == value;
113 /// Generates a hashcode for this object.
116 /// An Int32 value holding the hash code
118 public override int GetHashCode ()
120 // Guess there's not too many ways to hash a Boolean
121 return value ? 1 : 0;
125 /// Returns a given string as a boolean value. The string must be
126 /// equivalent to either TrueString or FalseString, with leading and/or
127 /// trailing spaces, and is parsed case-insensitively.
130 /// Throws an ArgumentNullException if <c>val</c> is null, or a
131 /// FormatException if <c>val</c> doesn't match <c>TrueString</c>
132 /// or <c>FalseString</c>
134 /// <param name="val">
135 /// The string value to parse
138 /// true if <c>val</c> is equivalent to TrueString,
141 public static bool Parse (string val)
144 throw new ArgumentNullException (
145 Locale.GetText ("Value is a null reference"));
149 if (String.Compare (val, TrueString, true) == 0)
152 if (String.Compare (val, FalseString, true) == 0)
155 throw new FormatException (Locale.GetText (
156 "Value is not equivalent to either TrueString or FalseString"));
160 /// Returns a string representation of this Boolean object.
163 /// <c>FalseString</c> if the instance value is false, otherwise
164 /// <c>TrueString</c>
166 public override string ToString ()
168 return value ? TrueString : FalseString;
171 // =========== IConvertible Methods =========== //
173 public TypeCode GetTypeCode ()
175 return TypeCode.Boolean;
178 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
180 return System.Convert.ToType(value, conversionType, provider);
183 bool IConvertible.ToBoolean (IFormatProvider provider)
188 byte IConvertible.ToByte (IFormatProvider provider)
190 return System.Convert.ToByte(value);
193 char IConvertible.ToChar (IFormatProvider provider)
195 throw new InvalidCastException();
198 [CLSCompliant(false)]
199 DateTime IConvertible.ToDateTime (IFormatProvider provider)
201 throw new InvalidCastException();
204 decimal IConvertible.ToDecimal (IFormatProvider provider)
206 return System.Convert.ToDecimal(value);
209 double IConvertible.ToDouble (IFormatProvider provider)
211 return System.Convert.ToDouble(value);
214 short IConvertible.ToInt16 (IFormatProvider provider)
216 return System.Convert.ToInt16(value);
219 int IConvertible.ToInt32 (IFormatProvider provider)
221 return System.Convert.ToInt32(value);
224 long IConvertible.ToInt64 (IFormatProvider provider)
226 return System.Convert.ToInt64(value);
229 [CLSCompliant(false)]
230 sbyte IConvertible.ToSByte (IFormatProvider provider)
232 return System.Convert.ToSByte(value);
235 float IConvertible.ToSingle (IFormatProvider provider)
237 return System.Convert.ToSingle(value);
240 public string ToString (IFormatProvider provider)
245 [CLSCompliant(false)]
246 ushort IConvertible.ToUInt16 (IFormatProvider provider)
248 return System.Convert.ToUInt16(value);
251 [CLSCompliant(false)]
252 uint IConvertible.ToUInt32 (IFormatProvider provider)
254 return System.Convert.ToUInt32(value);
257 [CLSCompliant(false)]
258 ulong IConvertible.ToUInt64 (IFormatProvider provider)
260 return System.Convert.ToUInt64(value);
265 } // Namespace System