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.
33 // The .NET Framework SDK lists this as implementing IConvertible,
34 // though it's not done in the ECMA spec.
35 public struct Boolean : IComparable { //, IConvertible {
38 /// The String representation of Boolean False
40 public static readonly string FalseString;
43 /// The String representation of Boolean True
45 public static readonly string TrueString;
48 /// Internal bool value for for this instance
51 // HACK: we tag it as public, so the source will compile.
56 FalseString = "False";
61 /// Compares the current Boolean instance against another object.
64 /// Throws an ArgumentException if <c>obj</c> isn't null or
67 /// <param name="obj">
68 /// The object to compare against
71 /// An int reflecting the sort order of this instance as
72 /// compared to <c>obj</c>
73 /// -1 if this instance is false and <c>obj</c> is true
74 /// 0 if this instance is equal to <c>obj</c>
75 /// 1 if this instance is true and <c>obj</c> is false,
76 /// or <c>obj</c> is null
78 public int CompareTo (object obj)
83 if (!(obj is System.Boolean))
84 throw new ArgumentException
85 (Locale.GetText ("Object is not a Boolean and is not a null reference"));
88 if (obj == null || (value == true && (bool)obj == false))
91 // for case #2, else it's #1
92 return (value == (bool)obj) ? 0 : -1;
96 /// Determines whether this instance and another object represent the
97 /// same type and value.
99 /// <param name="obj">
100 /// The object to check against
103 /// true if this instnace and <c>obj</c> are same value,
104 /// otherwise false if it is not or null
106 public override bool Equals (Object obj)
108 if (obj == null || !(obj is System.Boolean))
111 return ((bool)obj) == value;
115 /// Generates a hashcode for this object.
118 /// An Int32 value holding the hash code
120 public override int GetHashCode ()
122 // Guess there's not too many ways to hash a Boolean
123 return value ? 1 : 0;
127 /// Returns a given string as a boolean value. The string must be
128 /// equivalent to either TrueString or FalseString, with leading and/or
129 /// trailing spaces, and is parsed case-insensitively.
132 /// Throws an ArgumentNullException if <c>val</c> is null, or a
133 /// FormatException if <c>val</c> doesn't match <c>TrueString</c>
134 /// or <c>FalseString</c>
136 /// <param name="val">
137 /// The string value to parse
140 /// true if <c>val</c> is equivalent to TrueString,
143 public static bool Parse (string val)
146 throw new ArgumentNullException (
147 Locale.GetText ("Value is a null reference"));
151 if (String.Compare (val, TrueString, true) == 0)
154 if (String.Compare (val, FalseString, true) == 0)
157 throw new FormatException (Locale.GetText (
158 "Value is not equivalent to either TrueString or FalseString"));
162 /// Returns a string representation of this Boolean object.
165 /// <c>FalseString</c> if the instance value is false, otherwise
166 /// <c>TrueString</c>
168 public override string ToString ()
170 return value ? TrueString : FalseString;
173 // =========== IConvertible Methods =========== //
175 public TypeCode GetTypeCode ()
177 return TypeCode.Boolean;
182 } // Namespace System