2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System / Boolean.cs
blobc2222b314dd4777a0d67af7f7f8d8bcfb7406408
1 //
2 // System.Boolean.cs
3 //
4 // Author:
5 // Derek Holden (dholden@draper.com)
6 //
7 // (C) Ximian, Inc. http://www.ximian.com
8 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 // Testing SVN
34 // I guess this is the Boolean class. This was written word for word
35 // off the Library specification for System.Boolean in the ECMA
36 // TC39 TG2 and TG3 working documents.
38 // The XML style documentation isn't that elegant, but it seems to
39 // be the standard way according to the poorly documented C#
40 // Programmer's Reference section on XML Documentation.
43 using System.Globalization;
44 using System.Runtime.InteropServices;
46 namespace System
48 /// <summary>
49 /// Represents the boolean values of logical true and false.
50 /// </summary>
51 [Serializable]
52 [ComVisible (true)]
53 public struct Boolean : IComparable, IConvertible, IComparable <bool>, IEquatable <bool>
55 /// <value>
56 /// The String representation of Boolean False
57 /// </value>
58 public static readonly string FalseString = "False";
60 /// <value>
61 /// The String representation of Boolean True
62 /// </value>
63 public static readonly string TrueString = "True";
65 internal bool m_value;
67 /// <summary>
68 /// Compares the current Boolean instance against another object.
69 /// </summary>
70 /// <remarks>
71 /// Throws an ArgumentException if <c>obj</c> isn't null or
72 /// a Boolean.
73 /// </remarks>
74 /// <param name="obj">
75 /// The object to compare against
76 /// </param>
77 /// <returns>
78 /// An int reflecting the sort order of this instance as
79 /// compared to <c>obj</c>
80 /// -1 if this instance is false and <c>obj</c> is true
81 /// 0 if this instance is equal to <c>obj</c>
82 /// 1 if this instance is true and <c>obj</c> is false,
83 /// or <c>obj</c> is null
84 /// </returns>
85 public int CompareTo (object obj)
87 if (obj == null)
88 return 1;
90 if (!(obj is System.Boolean))
91 throw new ArgumentException (Locale.GetText (
92 "Object is not a Boolean."));
94 bool value = (bool) obj;
96 // for case #3
97 if (m_value && !value)
98 return 1;
100 // for case #2, else it's #1
101 return (m_value == value) ? 0 : -1;
104 /// <summary>
105 /// Determines whether this instance and another object represent the
106 /// same type and value.
107 /// </summary>
108 /// <param name="obj">
109 /// The object to check against
110 /// </param>
111 /// <returns>
112 /// true if this instnace and <c>obj</c> are same value,
113 /// otherwise false if it is not or null
114 /// </returns>
115 public override bool Equals (Object obj)
117 if (obj == null || !(obj is System.Boolean))
118 return false;
120 bool value = (bool) obj;
122 return m_value ? value : !value;
125 public int CompareTo (bool value)
127 if (m_value == value)
128 return 0;
129 return !m_value ? -1 : 1;
132 public bool Equals (bool obj)
134 return m_value == obj;
137 /// <summary>
138 /// Generates a hashcode for this object.
139 /// </summary>
140 /// <returns>
141 /// An Int32 value holding the hash code
142 /// </returns>
143 public override int GetHashCode ()
145 // Guess there's not too many ways to hash a Boolean
146 return m_value ? 1 : 0;
149 /// <summary>
150 /// Returns a given string as a boolean value. The string must be
151 /// equivalent to either TrueString or FalseString, with leading and/or
152 /// trailing spaces, and is parsed case-insensitively.
153 /// </summary>
154 /// <remarks>
155 /// Throws an ArgumentNullException if <c>val</c> is null, or a
156 /// FormatException if <c>val</c> doesn't match <c>TrueString</c>
157 /// or <c>FalseString</c>
158 /// </remarks>
159 /// <param name="val">
160 /// The string value to parse
161 /// </param>
162 /// <returns>
163 /// true if <c>val</c> is equivalent to TrueString,
164 /// otherwise false
165 /// </returns>
166 public static bool Parse (string value)
168 if (value == null)
169 throw new ArgumentNullException ("value");
171 value = value.Trim ();
173 if (String.Compare (value, TrueString, true, CultureInfo.InvariantCulture) == 0)
174 return true;
176 if (String.Compare (value, FalseString, true, CultureInfo.InvariantCulture) == 0)
177 return false;
179 throw new FormatException (Locale.GetText (
180 "Value is not equivalent to either TrueString or FalseString."));
183 public static bool TryParse (string value, out bool result)
185 result = false;
186 if (value == null)
187 return false;
189 value = value.Trim ();
191 if (String.Compare (value, TrueString, true, CultureInfo.InvariantCulture) == 0) {
192 result = true;
193 return true;
196 if (String.Compare (value, FalseString, true, CultureInfo.InvariantCulture) == 0) {
197 // result = false; // already set at false by default
198 return true;
201 return false;
204 /// <summary>
205 /// Returns a string representation of this Boolean object.
206 /// </summary>
207 /// <returns>
208 /// <c>FalseString</c> if the instance value is false, otherwise
209 /// <c>TrueString</c>
210 /// </returns>
211 public override string ToString ()
213 return m_value ? TrueString : FalseString;
216 // =========== IConvertible Methods =========== //
217 public TypeCode GetTypeCode ()
219 return TypeCode.Boolean;
222 object IConvertible.ToType (Type targetType, IFormatProvider provider)
224 if (targetType == null)
225 throw new ArgumentNullException ("targetType");
226 return System.Convert.ToType (m_value, targetType, provider, false);
229 bool IConvertible.ToBoolean (IFormatProvider provider)
231 return m_value;
234 byte IConvertible.ToByte (IFormatProvider provider)
236 return System.Convert.ToByte (m_value);
239 char IConvertible.ToChar (IFormatProvider provider)
241 throw new InvalidCastException ();
244 DateTime IConvertible.ToDateTime (IFormatProvider provider)
246 throw new InvalidCastException ();
249 decimal IConvertible.ToDecimal (IFormatProvider provider)
251 return System.Convert.ToDecimal (m_value);
254 double IConvertible.ToDouble (IFormatProvider provider)
256 return System.Convert.ToDouble (m_value);
259 short IConvertible.ToInt16 (IFormatProvider provider)
261 return System.Convert.ToInt16 (m_value);
264 int IConvertible.ToInt32 (IFormatProvider provider)
266 return System.Convert.ToInt32 (m_value);
269 long IConvertible.ToInt64 (IFormatProvider provider)
271 return System.Convert.ToInt64 (m_value);
274 sbyte IConvertible.ToSByte (IFormatProvider provider)
276 return System.Convert.ToSByte (m_value);
279 float IConvertible.ToSingle (IFormatProvider provider)
281 return System.Convert.ToSingle (m_value);
284 public string ToString (IFormatProvider provider)
286 return ToString ();
289 ushort IConvertible.ToUInt16 (IFormatProvider provider)
291 return System.Convert.ToUInt16 (m_value);
294 uint IConvertible.ToUInt32 (IFormatProvider provider)
296 return System.Convert.ToUInt32 (m_value);
299 ulong IConvertible.ToUInt64 (IFormatProvider provider)
301 return System.Convert.ToUInt64 (m_value);