UInt32.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. // Need to use compare because subtraction will wrap
  32. // to positive for very large neg numbers, etc.
  33. if (value is uint i)
  34. {
  35. if (m_value < i) return -1;
  36. if (m_value > i) return 1;
  37. return 0;
  38. }
  39. throw new ArgumentException(SR.Arg_MustBeUInt32);
  40. }
  41. public int CompareTo(uint value)
  42. {
  43. // Need to use compare because subtraction will wrap
  44. // to positive for very large neg numbers, etc.
  45. if (m_value < value) return -1;
  46. if (m_value > value) return 1;
  47. return 0;
  48. }
  49. public override bool Equals(object? obj)
  50. {
  51. if (!(obj is uint))
  52. {
  53. return false;
  54. }
  55. return m_value == ((uint)obj).m_value;
  56. }
  57. [NonVersionable]
  58. public bool Equals(uint obj)
  59. {
  60. return m_value == obj;
  61. }
  62. // The absolute value of the int contained.
  63. public override int GetHashCode()
  64. {
  65. return (int)m_value;
  66. }
  67. // The base 10 representation of the number with no extra padding.
  68. public override string ToString()
  69. {
  70. return Number.UInt32ToDecStr(m_value, -1);
  71. }
  72. public string ToString(IFormatProvider? provider)
  73. {
  74. return Number.FormatUInt32(m_value, null, provider);
  75. }
  76. public string ToString(string? format)
  77. {
  78. return Number.FormatUInt32(m_value, format, null);
  79. }
  80. public string ToString(string? format, IFormatProvider? provider)
  81. {
  82. return Number.FormatUInt32(m_value, format, provider);
  83. }
  84. public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
  85. {
  86. return Number.TryFormatUInt32(m_value, format, provider, destination, out charsWritten);
  87. }
  88. [CLSCompliant(false)]
  89. public static uint Parse(string s)
  90. {
  91. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  92. return Number.ParseUInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
  93. }
  94. [CLSCompliant(false)]
  95. public static uint Parse(string s, NumberStyles style)
  96. {
  97. NumberFormatInfo.ValidateParseStyleInteger(style);
  98. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  99. return Number.ParseUInt32(s, style, NumberFormatInfo.CurrentInfo);
  100. }
  101. [CLSCompliant(false)]
  102. public static uint Parse(string s, IFormatProvider? provider)
  103. {
  104. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  105. return Number.ParseUInt32(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
  106. }
  107. [CLSCompliant(false)]
  108. public static uint Parse(string s, NumberStyles style, IFormatProvider? provider)
  109. {
  110. NumberFormatInfo.ValidateParseStyleInteger(style);
  111. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  112. return Number.ParseUInt32(s, style, NumberFormatInfo.GetInstance(provider));
  113. }
  114. [CLSCompliant(false)]
  115. public static uint Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider? provider = null)
  116. {
  117. NumberFormatInfo.ValidateParseStyleInteger(style);
  118. return Number.ParseUInt32(s, style, NumberFormatInfo.GetInstance(provider));
  119. }
  120. [CLSCompliant(false)]
  121. public static bool TryParse(string? s, out uint result)
  122. {
  123. if (s == null)
  124. {
  125. result = 0;
  126. return false;
  127. }
  128. return Number.TryParseUInt32IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
  129. }
  130. [CLSCompliant(false)]
  131. public static bool TryParse(ReadOnlySpan<char> s, out uint result)
  132. {
  133. return Number.TryParseUInt32IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
  134. }
  135. [CLSCompliant(false)]
  136. public static bool TryParse(string? s, NumberStyles style, IFormatProvider? provider, out uint result)
  137. {
  138. NumberFormatInfo.ValidateParseStyleInteger(style);
  139. if (s == null)
  140. {
  141. result = 0;
  142. return false;
  143. }
  144. return Number.TryParseUInt32(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
  145. }
  146. [CLSCompliant(false)]
  147. public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out uint result)
  148. {
  149. NumberFormatInfo.ValidateParseStyleInteger(style);
  150. return Number.TryParseUInt32(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
  151. }
  152. //
  153. // IConvertible implementation
  154. //
  155. public TypeCode GetTypeCode()
  156. {
  157. return TypeCode.UInt32;
  158. }
  159. bool IConvertible.ToBoolean(IFormatProvider? provider)
  160. {
  161. return Convert.ToBoolean(m_value);
  162. }
  163. char IConvertible.ToChar(IFormatProvider? provider)
  164. {
  165. return Convert.ToChar(m_value);
  166. }
  167. sbyte IConvertible.ToSByte(IFormatProvider? provider)
  168. {
  169. return Convert.ToSByte(m_value);
  170. }
  171. byte IConvertible.ToByte(IFormatProvider? provider)
  172. {
  173. return Convert.ToByte(m_value);
  174. }
  175. short IConvertible.ToInt16(IFormatProvider? provider)
  176. {
  177. return Convert.ToInt16(m_value);
  178. }
  179. ushort IConvertible.ToUInt16(IFormatProvider? provider)
  180. {
  181. return Convert.ToUInt16(m_value);
  182. }
  183. int IConvertible.ToInt32(IFormatProvider? provider)
  184. {
  185. return Convert.ToInt32(m_value);
  186. }
  187. uint IConvertible.ToUInt32(IFormatProvider? provider)
  188. {
  189. return m_value;
  190. }
  191. long IConvertible.ToInt64(IFormatProvider? provider)
  192. {
  193. return Convert.ToInt64(m_value);
  194. }
  195. ulong IConvertible.ToUInt64(IFormatProvider? provider)
  196. {
  197. return Convert.ToUInt64(m_value);
  198. }
  199. float IConvertible.ToSingle(IFormatProvider? provider)
  200. {
  201. return Convert.ToSingle(m_value);
  202. }
  203. double IConvertible.ToDouble(IFormatProvider? provider)
  204. {
  205. return Convert.ToDouble(m_value);
  206. }
  207. decimal IConvertible.ToDecimal(IFormatProvider? provider)
  208. {
  209. return Convert.ToDecimal(m_value);
  210. }
  211. DateTime IConvertible.ToDateTime(IFormatProvider? provider)
  212. {
  213. throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "UInt32", "DateTime"));
  214. }
  215. object IConvertible.ToType(Type type, IFormatProvider? provider)
  216. {
  217. return Convert.DefaultToType((IConvertible)this, type, provider);
  218. }
  219. }
  220. }