UInt64.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 UInt64 : IComparable, IConvertible, IFormattable, IComparable<ulong>, IEquatable<ulong>, ISpanFormattable
  15. {
  16. private readonly ulong m_value; // Do not rename (binary serialization)
  17. public const ulong MaxValue = (ulong)0xffffffffffffffffL;
  18. public const ulong MinValue = 0x0;
  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 UInt64, 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 ulong 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_MustBeUInt64);
  40. }
  41. public int CompareTo(ulong 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 ulong))
  52. {
  53. return false;
  54. }
  55. return m_value == ((ulong)obj).m_value;
  56. }
  57. [NonVersionable]
  58. public bool Equals(ulong obj)
  59. {
  60. return m_value == obj;
  61. }
  62. // The value of the lower 32 bits XORed with the uppper 32 bits.
  63. public override int GetHashCode()
  64. {
  65. return ((int)m_value) ^ (int)(m_value >> 32);
  66. }
  67. public override string ToString()
  68. {
  69. return Number.UInt64ToDecStr(m_value, -1);
  70. }
  71. public string ToString(IFormatProvider? provider)
  72. {
  73. return Number.FormatUInt64(m_value, null, provider);
  74. }
  75. public string ToString(string? format)
  76. {
  77. return Number.FormatUInt64(m_value, format, null);
  78. }
  79. public string ToString(string? format, IFormatProvider? provider)
  80. {
  81. return Number.FormatUInt64(m_value, format, provider);
  82. }
  83. public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
  84. {
  85. return Number.TryFormatUInt64(m_value, format, provider, destination, out charsWritten);
  86. }
  87. [CLSCompliant(false)]
  88. public static ulong Parse(string s)
  89. {
  90. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  91. return Number.ParseUInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
  92. }
  93. [CLSCompliant(false)]
  94. public static ulong Parse(string s, NumberStyles style)
  95. {
  96. NumberFormatInfo.ValidateParseStyleInteger(style);
  97. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  98. return Number.ParseUInt64(s, style, NumberFormatInfo.CurrentInfo);
  99. }
  100. [CLSCompliant(false)]
  101. public static ulong Parse(string s, IFormatProvider? provider)
  102. {
  103. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  104. return Number.ParseUInt64(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
  105. }
  106. [CLSCompliant(false)]
  107. public static ulong Parse(string s, NumberStyles style, IFormatProvider? provider)
  108. {
  109. NumberFormatInfo.ValidateParseStyleInteger(style);
  110. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  111. return Number.ParseUInt64(s, style, NumberFormatInfo.GetInstance(provider));
  112. }
  113. [CLSCompliant(false)]
  114. public static ulong Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider? provider = null)
  115. {
  116. NumberFormatInfo.ValidateParseStyleInteger(style);
  117. return Number.ParseUInt64(s, style, NumberFormatInfo.GetInstance(provider));
  118. }
  119. [CLSCompliant(false)]
  120. public static bool TryParse(string? s, out ulong result)
  121. {
  122. if (s == null)
  123. {
  124. result = 0;
  125. return false;
  126. }
  127. return Number.TryParseUInt64IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
  128. }
  129. [CLSCompliant(false)]
  130. public static bool TryParse(ReadOnlySpan<char> s, out ulong result)
  131. {
  132. return Number.TryParseUInt64IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
  133. }
  134. [CLSCompliant(false)]
  135. public static bool TryParse(string? s, NumberStyles style, IFormatProvider? provider, out ulong result)
  136. {
  137. NumberFormatInfo.ValidateParseStyleInteger(style);
  138. if (s == null)
  139. {
  140. result = 0;
  141. return false;
  142. }
  143. return Number.TryParseUInt64(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
  144. }
  145. [CLSCompliant(false)]
  146. public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out ulong result)
  147. {
  148. NumberFormatInfo.ValidateParseStyleInteger(style);
  149. return Number.TryParseUInt64(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
  150. }
  151. //
  152. // IConvertible implementation
  153. //
  154. public TypeCode GetTypeCode()
  155. {
  156. return TypeCode.UInt64;
  157. }
  158. bool IConvertible.ToBoolean(IFormatProvider? provider)
  159. {
  160. return Convert.ToBoolean(m_value);
  161. }
  162. char IConvertible.ToChar(IFormatProvider? provider)
  163. {
  164. return Convert.ToChar(m_value);
  165. }
  166. sbyte IConvertible.ToSByte(IFormatProvider? provider)
  167. {
  168. return Convert.ToSByte(m_value);
  169. }
  170. byte IConvertible.ToByte(IFormatProvider? provider)
  171. {
  172. return Convert.ToByte(m_value);
  173. }
  174. short IConvertible.ToInt16(IFormatProvider? provider)
  175. {
  176. return Convert.ToInt16(m_value);
  177. }
  178. ushort IConvertible.ToUInt16(IFormatProvider? provider)
  179. {
  180. return Convert.ToUInt16(m_value);
  181. }
  182. int IConvertible.ToInt32(IFormatProvider? provider)
  183. {
  184. return Convert.ToInt32(m_value);
  185. }
  186. uint IConvertible.ToUInt32(IFormatProvider? provider)
  187. {
  188. return Convert.ToUInt32(m_value);
  189. }
  190. long IConvertible.ToInt64(IFormatProvider? provider)
  191. {
  192. return Convert.ToInt64(m_value);
  193. }
  194. ulong IConvertible.ToUInt64(IFormatProvider? provider)
  195. {
  196. return m_value;
  197. }
  198. float IConvertible.ToSingle(IFormatProvider? provider)
  199. {
  200. return Convert.ToSingle(m_value);
  201. }
  202. double IConvertible.ToDouble(IFormatProvider? provider)
  203. {
  204. return Convert.ToDouble(m_value);
  205. }
  206. decimal IConvertible.ToDecimal(IFormatProvider? provider)
  207. {
  208. return Convert.ToDecimal(m_value);
  209. }
  210. DateTime IConvertible.ToDateTime(IFormatProvider? provider)
  211. {
  212. throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "UInt64", "DateTime"));
  213. }
  214. object IConvertible.ToType(Type type, IFormatProvider? provider)
  215. {
  216. return Convert.DefaultToType((IConvertible)this, type, provider);
  217. }
  218. }
  219. }