Int64.cs 8.4 KB

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