UInt32.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.
  4. using System.Globalization;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. using System.Runtime.Versioning;
  8. namespace System
  9. {
  10. [Serializable]
  11. [CLSCompliant(false)]
  12. [StructLayout(LayoutKind.Sequential)]
  13. [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  14. public readonly struct UInt32 : IComparable, IConvertible, IFormattable, IComparable<uint>, IEquatable<uint>, ISpanFormattable
  15. {
  16. private readonly uint m_value; // Do not rename (binary serialization)
  17. public const uint MaxValue = (uint)0xffffffff;
  18. public const uint MinValue = 0U;
  19. // Compares this object to another object, returning an integer that
  20. // indicates the relationship.
  21. // Returns a value less than zero if this object
  22. // null is considered to be less than any instance.
  23. // If object is not of type UInt32, this method throws an ArgumentException.
  24. //
  25. public int CompareTo(object value)
  26. {
  27. if (value == null)
  28. {
  29. return 1;
  30. }
  31. if (value is uint)
  32. {
  33. // Need to use compare because subtraction will wrap
  34. // to positive for very large neg numbers, etc.
  35. uint i = (uint)value;
  36. if (m_value < i) return -1;
  37. if (m_value > i) return 1;
  38. return 0;
  39. }
  40. throw new ArgumentException(SR.Arg_MustBeUInt32);
  41. }
  42. public int CompareTo(uint value)
  43. {
  44. // Need to use compare because subtraction will wrap
  45. // to positive for very large neg numbers, etc.
  46. if (m_value < value) return -1;
  47. if (m_value > value) return 1;
  48. return 0;
  49. }
  50. public override bool Equals(object obj)
  51. {
  52. if (!(obj is uint))
  53. {
  54. return false;
  55. }
  56. return m_value == ((uint)obj).m_value;
  57. }
  58. [NonVersionable]
  59. public bool Equals(uint obj)
  60. {
  61. return m_value == obj;
  62. }
  63. // The absolute value of the int contained.
  64. public override int GetHashCode()
  65. {
  66. return ((int)m_value);
  67. }
  68. // The base 10 representation of the number with no extra padding.
  69. public override string ToString()
  70. {
  71. return Number.FormatUInt32(m_value, null, null);
  72. }
  73. public string ToString(IFormatProvider provider)
  74. {
  75. return Number.FormatUInt32(m_value, null, provider);
  76. }
  77. public string ToString(string format)
  78. {
  79. return Number.FormatUInt32(m_value, format, null);
  80. }
  81. public string ToString(string format, IFormatProvider provider)
  82. {
  83. return Number.FormatUInt32(m_value, format, provider);
  84. }
  85. public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
  86. {
  87. return Number.TryFormatUInt32(m_value, format, provider, destination, out charsWritten);
  88. }
  89. [CLSCompliant(false)]
  90. public static uint Parse(string s)
  91. {
  92. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  93. return Number.ParseUInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
  94. }
  95. [CLSCompliant(false)]
  96. public static uint Parse(string s, NumberStyles style)
  97. {
  98. NumberFormatInfo.ValidateParseStyleInteger(style);
  99. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  100. return Number.ParseUInt32(s, style, NumberFormatInfo.CurrentInfo);
  101. }
  102. [CLSCompliant(false)]
  103. public static uint Parse(string s, IFormatProvider provider)
  104. {
  105. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  106. return Number.ParseUInt32(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
  107. }
  108. [CLSCompliant(false)]
  109. public static uint Parse(string s, NumberStyles style, IFormatProvider provider)
  110. {
  111. NumberFormatInfo.ValidateParseStyleInteger(style);
  112. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  113. return Number.ParseUInt32(s, style, NumberFormatInfo.GetInstance(provider));
  114. }
  115. [CLSCompliant(false)]
  116. public static uint Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
  117. {
  118. NumberFormatInfo.ValidateParseStyleInteger(style);
  119. return Number.ParseUInt32(s, style, NumberFormatInfo.GetInstance(provider));
  120. }
  121. [CLSCompliant(false)]
  122. public static bool TryParse(string s, out uint result)
  123. {
  124. if (s == null)
  125. {
  126. result = 0;
  127. return false;
  128. }
  129. return Number.TryParseUInt32IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
  130. }
  131. [CLSCompliant(false)]
  132. public static bool TryParse(ReadOnlySpan<char> s, out uint result)
  133. {
  134. return Number.TryParseUInt32IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
  135. }
  136. [CLSCompliant(false)]
  137. public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out uint result)
  138. {
  139. NumberFormatInfo.ValidateParseStyleInteger(style);
  140. if (s == null)
  141. {
  142. result = 0;
  143. return false;
  144. }
  145. return Number.TryParseUInt32(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
  146. }
  147. [CLSCompliant(false)]
  148. public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out uint result)
  149. {
  150. NumberFormatInfo.ValidateParseStyleInteger(style);
  151. return Number.TryParseUInt32(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
  152. }
  153. //
  154. // IConvertible implementation
  155. //
  156. public TypeCode GetTypeCode()
  157. {
  158. return TypeCode.UInt32;
  159. }
  160. bool IConvertible.ToBoolean(IFormatProvider provider)
  161. {
  162. return Convert.ToBoolean(m_value);
  163. }
  164. char IConvertible.ToChar(IFormatProvider provider)
  165. {
  166. return Convert.ToChar(m_value);
  167. }
  168. sbyte IConvertible.ToSByte(IFormatProvider provider)
  169. {
  170. return Convert.ToSByte(m_value);
  171. }
  172. byte IConvertible.ToByte(IFormatProvider provider)
  173. {
  174. return Convert.ToByte(m_value);
  175. }
  176. short IConvertible.ToInt16(IFormatProvider provider)
  177. {
  178. return Convert.ToInt16(m_value);
  179. }
  180. ushort IConvertible.ToUInt16(IFormatProvider provider)
  181. {
  182. return Convert.ToUInt16(m_value);
  183. }
  184. int IConvertible.ToInt32(IFormatProvider provider)
  185. {
  186. return Convert.ToInt32(m_value);
  187. }
  188. uint IConvertible.ToUInt32(IFormatProvider provider)
  189. {
  190. return m_value;
  191. }
  192. long IConvertible.ToInt64(IFormatProvider provider)
  193. {
  194. return Convert.ToInt64(m_value);
  195. }
  196. ulong IConvertible.ToUInt64(IFormatProvider provider)
  197. {
  198. return Convert.ToUInt64(m_value);
  199. }
  200. float IConvertible.ToSingle(IFormatProvider provider)
  201. {
  202. return Convert.ToSingle(m_value);
  203. }
  204. double IConvertible.ToDouble(IFormatProvider provider)
  205. {
  206. return Convert.ToDouble(m_value);
  207. }
  208. decimal IConvertible.ToDecimal(IFormatProvider provider)
  209. {
  210. return Convert.ToDecimal(m_value);
  211. }
  212. DateTime IConvertible.ToDateTime(IFormatProvider provider)
  213. {
  214. throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "UInt32", "DateTime"));
  215. }
  216. object IConvertible.ToType(Type type, IFormatProvider provider)
  217. {
  218. return Convert.DefaultToType((IConvertible)this, type, provider);
  219. }
  220. }
  221. }