3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
11 ** Purpose: The boolean class serves as a wrapper for the primitive
15 ===========================================================*/
19 using System.Globalization;
20 using System.Diagnostics.Contracts;
21 // The Boolean class provides the
22 // object representation of the boolean primitive type.
24 [System.Runtime.InteropServices.ComVisible(true)]
25 public struct Boolean : IComparable, IConvertible
27 , IComparable<Boolean>, IEquatable<Boolean>
30 #pragma warning disable 649
35 #pragma warning disable
39 internal const int True = 1;
43 internal const int False = 0;
47 // Internal Constants are real consts for performance.
50 // The internal string representation of true.
52 internal const String TrueLiteral = "True";
54 // The internal string representation of false.
56 internal const String FalseLiteral = "False";
63 // The public string representation of true.
65 public static readonly String TrueString = TrueLiteral;
67 // The public string representation of false.
69 public static readonly String FalseString = FalseLiteral;
72 // Overriden Instance Methods
74 /*=================================GetHashCode==================================
76 **Returns: 1 or 0 depending on whether this instance represents true or false.
78 **Overriden From: Value
79 ==============================================================================*/
80 // Provides a hash code for this instance.
81 public override int GetHashCode() {
82 return (m_value)?True:False;
85 /*===================================ToString===================================
87 **Returns: "True" or "False" depending on the state of the boolean.
89 ==============================================================================*/
90 // Converts the boolean value of this instance to a String.
91 public override String ToString() {
92 if (false == m_value) {
98 public String ToString(IFormatProvider provider) {
99 if (false == m_value) {
105 // Determines whether two Boolean objects are equal.
106 public override bool Equals (Object obj) {
107 //If it's not a boolean, we're definitely not equal
108 if (!(obj is Boolean)) {
112 return (m_value==((Boolean)obj).m_value);
115 [System.Runtime.Versioning.NonVersionable]
116 public bool Equals(Boolean obj)
118 return m_value == obj;
121 // Compares this object to another object, returning an integer that
122 // indicates the relationship. For booleans, false sorts before true.
123 // null is considered to be less than any instance.
124 // If object is not of type boolean, this method throws an ArgumentException.
126 // Returns a value less than zero if this object
128 public int CompareTo(Object obj) {
132 if (!(obj is Boolean)) {
133 throw new ArgumentException (Environment.GetResourceString("Arg_MustBeBoolean"));
136 if (m_value==((Boolean)obj).m_value) {
138 } else if (m_value==false) {
144 public int CompareTo(Boolean value) {
145 if (m_value==value) {
147 } else if (m_value==false) {
157 // Determines whether a String represents true or false.
159 public static Boolean Parse (String value) {
160 if (value==null) throw new ArgumentNullException("value");
161 Contract.EndContractBlock();
162 Boolean result = false;
163 if (!TryParse(value, out result)) {
164 throw new FormatException(Environment.GetResourceString("Format_BadBoolean"));
171 // Determines whether a String represents true or false.
173 public static Boolean TryParse (String value, out Boolean result) {
178 // For perf reasons, let's first see if they're equal, then do the
179 // trim to get rid of white space, and check again.
180 if (TrueLiteral.Equals(value, StringComparison.OrdinalIgnoreCase)) {
184 if (FalseLiteral.Equals(value,StringComparison.OrdinalIgnoreCase)) {
189 // Special case: Trim whitespace as well as null characters.
190 value = TrimWhiteSpaceAndNull(value);
192 if (TrueLiteral.Equals(value, StringComparison.OrdinalIgnoreCase)) {
197 if (FalseLiteral.Equals(value,StringComparison.OrdinalIgnoreCase)) {
205 private static String TrimWhiteSpaceAndNull(String value) {
207 int end = value.Length-1;
208 char nullChar = (char) 0x0000;
210 while (start < value.Length) {
211 if (!Char.IsWhiteSpace(value[start]) && value[start] != nullChar) {
217 while (end >= start) {
218 if (!Char.IsWhiteSpace(value[end]) && value[end] != nullChar) {
224 return value.Substring(start, end - start + 1);
228 // IConvertible implementation
231 public TypeCode GetTypeCode() {
232 return TypeCode.Boolean;
237 bool IConvertible.ToBoolean(IFormatProvider provider) {
242 char IConvertible.ToChar(IFormatProvider provider) {
243 throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Boolean", "Char"));
247 sbyte IConvertible.ToSByte(IFormatProvider provider) {
248 return Convert.ToSByte(m_value);
252 byte IConvertible.ToByte(IFormatProvider provider) {
253 return Convert.ToByte(m_value);
257 short IConvertible.ToInt16(IFormatProvider provider) {
258 return Convert.ToInt16(m_value);
262 ushort IConvertible.ToUInt16(IFormatProvider provider) {
263 return Convert.ToUInt16(m_value);
267 int IConvertible.ToInt32(IFormatProvider provider) {
268 return Convert.ToInt32(m_value);
272 uint IConvertible.ToUInt32(IFormatProvider provider) {
273 return Convert.ToUInt32(m_value);
277 long IConvertible.ToInt64(IFormatProvider provider) {
278 return Convert.ToInt64(m_value);
282 ulong IConvertible.ToUInt64(IFormatProvider provider) {
283 return Convert.ToUInt64(m_value);
287 float IConvertible.ToSingle(IFormatProvider provider) {
288 return Convert.ToSingle(m_value);
292 double IConvertible.ToDouble(IFormatProvider provider) {
293 return Convert.ToDouble(m_value);
297 Decimal IConvertible.ToDecimal(IFormatProvider provider) {
298 return Convert.ToDecimal(m_value);
302 DateTime IConvertible.ToDateTime(IFormatProvider provider) {
303 throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Boolean", "DateTime"));
307 Object IConvertible.ToType(Type type, IFormatProvider provider) {
308 return Convert.DefaultToType((IConvertible)this, type, provider);