2
0

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. if (value is long)
  31. {
  32. // Need to use compare because subtraction will wrap
  33. // to positive for very large neg numbers, etc.
  34. long i = (long)value;
  35. if (m_value < i) return -1;
  36. if (m_value > i) return 1;
  37. return 0;
  38. }
  39. throw new ArgumentException(SR.Arg_MustBeInt64);
  40. }
  41. public int CompareTo(long 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 long))
  52. {
  53. return false;
  54. }
  55. return m_value == ((long)obj).m_value;
  56. }
  57. [NonVersionable]
  58. public bool Equals(long 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 (unchecked((int)((long)m_value)) ^ (int)(m_value >> 32));
  66. }
  67. public override string ToString()
  68. {
  69. return Number.FormatInt64(m_value, null, null);
  70. }
  71. public string ToString(IFormatProvider provider)
  72. {
  73. return Number.FormatInt64(m_value, null, provider);
  74. }
  75. public string ToString(string format)
  76. {
  77. return Number.FormatInt64(m_value, format, null);
  78. }
  79. public string ToString(string format, IFormatProvider provider)
  80. {
  81. return Number.FormatInt64(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.TryFormatInt64(m_value, format, provider, destination, out charsWritten);
  86. }
  87. public static long Parse(string s)
  88. {
  89. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  90. return Number.ParseInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
  91. }
  92. public static long Parse(string s, NumberStyles style)
  93. {
  94. NumberFormatInfo.ValidateParseStyleInteger(style);
  95. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  96. return Number.ParseInt64(s, style, NumberFormatInfo.CurrentInfo);
  97. }
  98. public static long Parse(string s, IFormatProvider provider)
  99. {
  100. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  101. return Number.ParseInt64(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
  102. }
  103. // Parses a long from a String in the given style. If
  104. // a NumberFormatInfo isn't specified, the current culture's
  105. // NumberFormatInfo is assumed.
  106. //
  107. public static long Parse(string s, NumberStyles style, IFormatProvider provider)
  108. {
  109. NumberFormatInfo.ValidateParseStyleInteger(style);
  110. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  111. return Number.ParseInt64(s, style, NumberFormatInfo.GetInstance(provider));
  112. }
  113. public static long Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
  114. {
  115. NumberFormatInfo.ValidateParseStyleInteger(style);
  116. return Number.ParseInt64(s, style, NumberFormatInfo.GetInstance(provider));
  117. }
  118. public static bool TryParse(string s, out long result)
  119. {
  120. if (s == null)
  121. {
  122. result = 0;
  123. return false;
  124. }
  125. return Number.TryParseInt64IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
  126. }
  127. public static bool TryParse(ReadOnlySpan<char> s, out long result)
  128. {
  129. return Number.TryParseInt64IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
  130. }
  131. public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out long result)
  132. {
  133. NumberFormatInfo.ValidateParseStyleInteger(style);
  134. if (s == null)
  135. {
  136. result = 0;
  137. return false;
  138. }
  139. return Number.TryParseInt64(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
  140. }
  141. public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out long result)
  142. {
  143. NumberFormatInfo.ValidateParseStyleInteger(style);
  144. return Number.TryParseInt64(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
  145. }
  146. //
  147. // IConvertible implementation
  148. //
  149. public TypeCode GetTypeCode()
  150. {
  151. return TypeCode.Int64;
  152. }
  153. bool IConvertible.ToBoolean(IFormatProvider provider)
  154. {
  155. return Convert.ToBoolean(m_value);
  156. }
  157. char IConvertible.ToChar(IFormatProvider provider)
  158. {
  159. return Convert.ToChar(m_value);
  160. }
  161. sbyte IConvertible.ToSByte(IFormatProvider provider)
  162. {
  163. return Convert.ToSByte(m_value);
  164. }
  165. byte IConvertible.ToByte(IFormatProvider provider)
  166. {
  167. return Convert.ToByte(m_value);
  168. }
  169. short IConvertible.ToInt16(IFormatProvider provider)
  170. {
  171. return Convert.ToInt16(m_value);
  172. }
  173. ushort IConvertible.ToUInt16(IFormatProvider provider)
  174. {
  175. return Convert.ToUInt16(m_value);
  176. }
  177. int IConvertible.ToInt32(IFormatProvider provider)
  178. {
  179. return Convert.ToInt32(m_value);
  180. }
  181. uint IConvertible.ToUInt32(IFormatProvider provider)
  182. {
  183. return Convert.ToUInt32(m_value);
  184. }
  185. long IConvertible.ToInt64(IFormatProvider provider)
  186. {
  187. return m_value;
  188. }
  189. ulong IConvertible.ToUInt64(IFormatProvider provider)
  190. {
  191. return Convert.ToUInt64(m_value);
  192. }
  193. float IConvertible.ToSingle(IFormatProvider provider)
  194. {
  195. return Convert.ToSingle(m_value);
  196. }
  197. double IConvertible.ToDouble(IFormatProvider provider)
  198. {
  199. return Convert.ToDouble(m_value);
  200. }
  201. decimal IConvertible.ToDecimal(IFormatProvider provider)
  202. {
  203. return Convert.ToDecimal(m_value);
  204. }
  205. DateTime IConvertible.ToDateTime(IFormatProvider provider)
  206. {
  207. throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Int64", "DateTime"));
  208. }
  209. object IConvertible.ToType(Type type, IFormatProvider provider)
  210. {
  211. return Convert.DefaultToType((IConvertible)this, type, provider);
  212. }
  213. }
  214. }