UInt64.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. if (value is ulong)
  32. {
  33. // Need to use compare because subtraction will wrap
  34. // to positive for very large neg numbers, etc.
  35. ulong i = (ulong)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_MustBeUInt64);
  41. }
  42. public int CompareTo(ulong 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 ulong))
  53. {
  54. return false;
  55. }
  56. return m_value == ((ulong)obj).m_value;
  57. }
  58. [NonVersionable]
  59. public bool Equals(ulong obj)
  60. {
  61. return m_value == obj;
  62. }
  63. // The value of the lower 32 bits XORed with the uppper 32 bits.
  64. public override int GetHashCode()
  65. {
  66. return ((int)m_value) ^ (int)(m_value >> 32);
  67. }
  68. public override string ToString()
  69. {
  70. return Number.FormatUInt64(m_value, null, null);
  71. }
  72. public string ToString(IFormatProvider provider)
  73. {
  74. return Number.FormatUInt64(m_value, null, provider);
  75. }
  76. public string ToString(string format)
  77. {
  78. return Number.FormatUInt64(m_value, format, null);
  79. }
  80. public string ToString(string format, IFormatProvider provider)
  81. {
  82. return Number.FormatUInt64(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.TryFormatUInt64(m_value, format, provider, destination, out charsWritten);
  87. }
  88. [CLSCompliant(false)]
  89. public static ulong Parse(string s)
  90. {
  91. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  92. return Number.ParseUInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
  93. }
  94. [CLSCompliant(false)]
  95. public static ulong Parse(string s, NumberStyles style)
  96. {
  97. NumberFormatInfo.ValidateParseStyleInteger(style);
  98. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  99. return Number.ParseUInt64(s, style, NumberFormatInfo.CurrentInfo);
  100. }
  101. [CLSCompliant(false)]
  102. public static ulong Parse(string s, IFormatProvider provider)
  103. {
  104. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  105. return Number.ParseUInt64(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
  106. }
  107. [CLSCompliant(false)]
  108. public static ulong Parse(string s, NumberStyles style, IFormatProvider provider)
  109. {
  110. NumberFormatInfo.ValidateParseStyleInteger(style);
  111. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  112. return Number.ParseUInt64(s, style, NumberFormatInfo.GetInstance(provider));
  113. }
  114. [CLSCompliant(false)]
  115. public static ulong Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
  116. {
  117. NumberFormatInfo.ValidateParseStyleInteger(style);
  118. return Number.ParseUInt64(s, style, NumberFormatInfo.GetInstance(provider));
  119. }
  120. [CLSCompliant(false)]
  121. public static bool TryParse(string s, out ulong result)
  122. {
  123. if (s == null)
  124. {
  125. result = 0;
  126. return false;
  127. }
  128. return Number.TryParseUInt64IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
  129. }
  130. [CLSCompliant(false)]
  131. public static bool TryParse(ReadOnlySpan<char> s, out ulong result)
  132. {
  133. return Number.TryParseUInt64IntegerStyle(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 ulong result)
  137. {
  138. NumberFormatInfo.ValidateParseStyleInteger(style);
  139. if (s == null)
  140. {
  141. result = 0;
  142. return false;
  143. }
  144. return Number.TryParseUInt64(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 ulong result)
  148. {
  149. NumberFormatInfo.ValidateParseStyleInteger(style);
  150. return Number.TryParseUInt64(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
  151. }
  152. //
  153. // IConvertible implementation
  154. //
  155. public TypeCode GetTypeCode()
  156. {
  157. return TypeCode.UInt64;
  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 Convert.ToUInt32(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 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, "UInt64", "DateTime"));
  214. }
  215. object IConvertible.ToType(Type type, IFormatProvider provider)
  216. {
  217. return Convert.DefaultToType((IConvertible)this, type, provider);
  218. }
  219. }
  220. }