1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 /*============================================================
9 ** Purpose: The boolean class serves as a wrapper for the primitive
13 ===========================================================*/
15 using System.Diagnostics;
16 using System.Runtime.CompilerServices;
17 using System.Runtime.Versioning;
22 [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
23 public struct Boolean : IComparable, IConvertible, IComparable<Boolean>, IEquatable<Boolean>
28 private bool m_value; // Do not rename (binary serialization)
32 internal const int True = 1;
36 internal const int False = 0;
40 // Internal Constants are real consts for performance.
43 // The internal string representation of true.
45 internal const String TrueLiteral = "True";
47 // The internal string representation of false.
49 internal const String FalseLiteral = "False";
56 // The public string representation of true.
58 public static readonly String TrueString = TrueLiteral;
60 // The public string representation of false.
62 public static readonly String FalseString = FalseLiteral;
65 // Overriden Instance Methods
67 /*=================================GetHashCode==================================
69 **Returns: 1 or 0 depending on whether this instance represents true or false.
71 **Overriden From: Value
72 ==============================================================================*/
73 // Provides a hash code for this instance.
74 public override int GetHashCode()
76 return (m_value) ? True : False;
79 /*===================================ToString===================================
81 **Returns: "True" or "False" depending on the state of the boolean.
83 ==============================================================================*/
84 // Converts the boolean value of this instance to a String.
85 public override String ToString()
94 public String ToString(IFormatProvider provider)
99 public bool TryFormat(Span<char> destination, out int charsWritten)
101 string s = m_value ? TrueLiteral : FalseLiteral;
103 if (s.AsSpan().TryCopyTo(destination))
105 charsWritten = s.Length;
115 // Determines whether two Boolean objects are equal.
116 public override bool Equals(Object obj)
118 //If it's not a boolean, we're definitely not equal
119 if (!(obj is Boolean))
124 return (m_value == ((Boolean)obj).m_value);
128 public bool Equals(Boolean obj)
130 return m_value == obj;
133 // Compares this object to another object, returning an integer that
134 // indicates the relationship. For booleans, false sorts before true.
135 // null is considered to be less than any instance.
136 // If object is not of type boolean, this method throws an ArgumentException.
138 // Returns a value less than zero if this object
140 public int CompareTo(Object obj)
146 if (!(obj is Boolean))
148 throw new ArgumentException(SR.Arg_MustBeBoolean);
151 if (m_value == ((Boolean)obj).m_value)
155 else if (m_value == false)
162 public int CompareTo(Boolean value)
164 if (m_value == value)
168 else if (m_value == false)
179 // Determines whether a String represents true or false.
181 public static Boolean Parse(String value)
183 if (value == null) throw new ArgumentNullException(nameof(value));
184 return Parse(value.AsSpan());
187 public static bool Parse(ReadOnlySpan<char> value) =>
188 TryParse(value, out bool result) ? result : throw new FormatException(SR.Format_BadBoolean);
190 // Determines whether a String represents true or false.
192 public static Boolean TryParse(String value, out Boolean result)
200 return TryParse(value.AsSpan(), out result);
203 public static bool TryParse(ReadOnlySpan<char> value, out bool result)
205 ReadOnlySpan<char> trueSpan = TrueLiteral.AsSpan();
206 if (StringSpanHelpers.Equals(trueSpan, value, StringComparison.OrdinalIgnoreCase))
212 ReadOnlySpan<char> falseSpan = FalseLiteral.AsSpan();
213 if (StringSpanHelpers.Equals(falseSpan, value, StringComparison.OrdinalIgnoreCase))
219 // Special case: Trim whitespace as well as null characters.
220 value = TrimWhiteSpaceAndNull(value);
222 if (StringSpanHelpers.Equals(trueSpan, value, StringComparison.OrdinalIgnoreCase))
228 if (StringSpanHelpers.Equals(falseSpan, value, StringComparison.OrdinalIgnoreCase))
238 private static ReadOnlySpan<char> TrimWhiteSpaceAndNull(ReadOnlySpan<char> value)
240 const char nullChar = (char)0x0000;
243 while (start < value.Length)
245 if (!Char.IsWhiteSpace(value[start]) && value[start] != nullChar)
252 int end = value.Length - 1;
255 if (!Char.IsWhiteSpace(value[end]) && value[end] != nullChar)
262 return value.Slice(start, end - start + 1);
266 // IConvertible implementation
269 public TypeCode GetTypeCode()
271 return TypeCode.Boolean;
275 bool IConvertible.ToBoolean(IFormatProvider provider)
280 char IConvertible.ToChar(IFormatProvider provider)
282 throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Boolean", "Char"));
285 sbyte IConvertible.ToSByte(IFormatProvider provider)
287 return Convert.ToSByte(m_value);
290 byte IConvertible.ToByte(IFormatProvider provider)
292 return Convert.ToByte(m_value);
295 short IConvertible.ToInt16(IFormatProvider provider)
297 return Convert.ToInt16(m_value);
300 ushort IConvertible.ToUInt16(IFormatProvider provider)
302 return Convert.ToUInt16(m_value);
305 int IConvertible.ToInt32(IFormatProvider provider)
307 return Convert.ToInt32(m_value);
310 uint IConvertible.ToUInt32(IFormatProvider provider)
312 return Convert.ToUInt32(m_value);
315 long IConvertible.ToInt64(IFormatProvider provider)
317 return Convert.ToInt64(m_value);
320 ulong IConvertible.ToUInt64(IFormatProvider provider)
322 return Convert.ToUInt64(m_value);
325 float IConvertible.ToSingle(IFormatProvider provider)
327 return Convert.ToSingle(m_value);
330 double IConvertible.ToDouble(IFormatProvider provider)
332 return Convert.ToDouble(m_value);
335 Decimal IConvertible.ToDecimal(IFormatProvider provider)
337 return Convert.ToDecimal(m_value);
340 DateTime IConvertible.ToDateTime(IFormatProvider provider)
342 throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Boolean", "DateTime"));
345 Object IConvertible.ToType(Type type, IFormatProvider provider)
347 return Convert.DefaultToType((IConvertible)this, type, provider);