Boolean.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // System.Boolean.cs
  3. //
  4. // Author:
  5. // Derek Holden ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. //
  9. //
  10. // I guess this is the Boolean class. This was written word for word
  11. // off the Library specification for System.Boolean in the ECMA
  12. // TC39 TG2 and TG3 working documents.
  13. //
  14. // The XML style documentation isn't that elegant, but it seems to
  15. // be the standard way according to the poorly documented C#
  16. // Programmer's Reference section on XML Documentation.
  17. //
  18. // This header and the one above it can be formatted however, just trying
  19. // to keep it consistent w/ the existing mcs headers.
  20. //
  21. // Even though it's not in the ECMA docs, the .NET Framework Class Library
  22. // says this implements IConvertible, but if it does it has some other
  23. // member functions to implement.
  24. //
  25. namespace System {
  26. /// <summary>
  27. /// Represents the boolean values of logical true and false.
  28. /// </summary>
  29. // The .NET Framework SDK lists this as implementing IConvertible,
  30. // though it's not done in the ECMA spec.
  31. public struct Boolean : IComparable { //, IConvertible {
  32. /// <value>
  33. /// The String representation of Boolean False
  34. /// </value>
  35. public static readonly string FalseString;
  36. /// <value>
  37. /// The String representation of Boolean True
  38. /// </value>
  39. public static readonly string TrueString;
  40. /// <value>
  41. /// Internal bool value for for this instance
  42. /// </value>
  43. //
  44. // HACK: we tag it as public, so the source will compile.
  45. public bool value;
  46. static Boolean ()
  47. {
  48. FalseString = "False";
  49. TrueString = "True";
  50. }
  51. /// <summary>
  52. /// Compares the current Boolean instance against another object.
  53. /// </summary>
  54. /// <remarks>
  55. /// Throws an ArgumentException if <c>obj</c> isn't null or
  56. /// a Boolean.
  57. /// </remarks>
  58. /// <param name="obj">
  59. /// The object to compare against
  60. /// </param>
  61. /// <returns>
  62. /// An int reflecting the sort order of this instance as
  63. /// compared to <c>obj</c>
  64. /// -1 if this instance is false and <c>obj</c> is true
  65. /// 0 if this instance is equal to <c>obj</c>
  66. /// 1 if this instance is true and <c>obj</c> is false,
  67. /// or <c>obj</c> is null
  68. /// </returns>
  69. public int CompareTo (object obj)
  70. {
  71. if(obj != null && !(obj is System.Boolean))
  72. throw new ArgumentException
  73. ("Object is not a Boolean and is not a null reference");
  74. // for case #3
  75. if (obj == null || (value == true && (bool)obj == false))
  76. return 1;
  77. // for case #2, else it's #1
  78. return (value == (bool)obj) ? 0 : -1;
  79. }
  80. /// <summary>
  81. /// Determines whether this instance and another object represent the
  82. /// same type and value.
  83. /// </summary>
  84. /// <param name="obj">
  85. /// The object to check against
  86. /// </param>
  87. /// <returns>
  88. /// true if this instnace and <c>obj</c> are same value,
  89. /// otherwise false if it is not or null
  90. /// </returns>
  91. public override bool Equals (Object obj)
  92. {
  93. if (obj == null || !(obj is System.Boolean))
  94. return false;
  95. return ((bool)obj) == value;
  96. }
  97. /// <summary>
  98. /// Generates a hashcode for this object.
  99. /// </summary>
  100. /// <returns>
  101. /// An Int32 value holding the hash code
  102. /// </returns>
  103. public override int GetHashCode ()
  104. {
  105. // Guess there's not too many ways to hash a Boolean
  106. return value ? 1 : 0;
  107. }
  108. /// <summary>
  109. /// Returns a given string as a boolean value. The string must be
  110. /// equivalent to either TrueString or FalseString, with leading and/or
  111. /// trailing spaces, and is parsed case-insensitively.
  112. /// </summary>
  113. /// <remarks>
  114. /// Throws an ArgumentNullException if <c>val</c> is null, or a
  115. /// FormatException if <c>val</c> doesn't match <c>TrueString</c>
  116. /// or <c>FalseString</c>
  117. /// </remarks>
  118. /// <param name="val">
  119. /// The string value to parse
  120. /// </param>
  121. /// <returns>
  122. /// true if <c>val</c> is equivalent to TrueString,
  123. /// otherwise false
  124. /// </returns>
  125. public static bool Parse (string val)
  126. {
  127. if (val == null)
  128. throw new ArgumentNullException ("Value is a null reference");
  129. val = val.Trim ();
  130. if (String.Compare (val, TrueString, true) == 0)
  131. return true;
  132. if (String.Compare (val, FalseString, true) == 0)
  133. return false;
  134. throw new FormatException
  135. ("Value is not equivalent to either TrueString or FalseString");
  136. }
  137. /// <summary>
  138. /// Returns a string representation of this Boolean object.
  139. /// </summary>
  140. /// <returns>
  141. /// <c>FalseString</c> if the instance value is false, otherwise
  142. /// <c>TrueString</c>
  143. /// </returns>
  144. public override string ToString ()
  145. {
  146. return value ? TrueString : FalseString;
  147. }
  148. // =========== IConvertible Methods =========== //
  149. public TypeCode GetTypeCode ()
  150. {
  151. return TypeCode.Boolean;
  152. }
  153. } // System.Boolean
  154. } // Namespace System