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.
30 /// Represents the boolean values of logical true and false.
32 // The .NET Framework SDK lists this as implementing IConvertible,
33 // though it's not done in the ECMA spec.
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
50 // HACK: we tag it as public, so the source will compile.
55 FalseString = "False";
60 /// Compares the current Boolean instance against another object.
63 /// Throws an ArgumentException if <c>obj</c> isn't null or
66 /// <param name="obj">
67 /// The object to compare against
70 /// An int reflecting the sort order of this instance as
71 /// compared to <c>obj</c>
72 /// -1 if this instance is false and <c>obj</c> is true
73 /// 0 if this instance is equal to <c>obj</c>
74 /// 1 if this instance is true and <c>obj</c> is false,
75 /// or <c>obj</c> is null
77 public int CompareTo (object obj)
79 if(obj != null && !(obj is System.Boolean))
80 throw new ArgumentException
81 ("Object is not a Boolean and is not a null reference");
84 if (obj == null || (value == true && (bool)obj == false))
87 // for case #2, else it's #1
88 return (value == (bool)obj) ? 0 : -1;
92 /// Determines whether this instance and another object represent the
93 /// same type and value.
95 /// <param name="obj">
96 /// The object to check against
99 /// true if this instnace and <c>obj</c> are same value,
100 /// otherwise false if it is not or null
102 public override bool Equals (Object obj)
104 if (obj == null || !(obj is System.Boolean))
107 return ((bool)obj) == value;
111 /// Generates a hashcode for this object.
114 /// An Int32 value holding the hash code
116 public override int GetHashCode ()
118 // Guess there's not too many ways to hash a Boolean
119 return value ? 1 : 0;
123 /// Returns a given string as a boolean value. The string must be
124 /// equivalent to either TrueString or FalseString, with leading and/or
125 /// trailing spaces, and is parsed case-insensitively.
128 /// Throws an ArgumentNullException if <c>val</c> is null, or a
129 /// FormatException if <c>val</c> doesn't match <c>TrueString</c>
130 /// or <c>FalseString</c>
132 /// <param name="val">
133 /// The string value to parse
136 /// true if <c>val</c> is equivalent to TrueString,
139 public static bool Parse (string val)
142 throw new ArgumentNullException ("Value is a null reference");
146 if (String.Compare (val, TrueString, true) == 0)
149 if (String.Compare (val, FalseString, true) == 0)
152 throw new FormatException
153 ("Value is not equivalent to either TrueString or FalseString");
157 /// Returns a string representation of this Boolean object.
160 /// <c>FalseString</c> if the instance value is false, otherwise
161 /// <c>TrueString</c>
163 public override string ToString ()
165 return value ? TrueString : FalseString;
168 // =========== IConvertible Methods =========== //
170 public TypeCode GetTypeCode ()
172 return TypeCode.Boolean;
177 } // Namespace System