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.
5 using System
.Globalization
;
6 using System
.Runtime
.CompilerServices
;
7 using System
.Runtime
.InteropServices
;
8 using System
.Runtime
.Versioning
;
13 [StructLayout(LayoutKind
.Sequential
)]
14 [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
15 public readonly struct Int64
: IComparable
, IConvertible
, IFormattable
, IComparable
<long>, IEquatable
<long>, ISpanFormattable
17 private readonly long m_value
; // Do not rename (binary serialization)
19 public const long MaxValue
= 0x7fffffffffffffffL
;
20 public const long MinValue
= unchecked((long)0x8000000000000000L
);
22 // Compares this object to another object, returning an integer that
23 // indicates the relationship.
24 // Returns a value less than zero if this object
25 // null is considered to be less than any instance.
26 // If object is not of type Int64, this method throws an ArgumentException.
28 public int CompareTo(object? value)
35 // Need to use compare because subtraction will wrap
36 // to positive for very large neg numbers, etc.
39 if (m_value
< i
) return -1;
40 if (m_value
> i
) return 1;
44 throw new ArgumentException(SR
.Arg_MustBeInt64
);
47 public int CompareTo(long value)
49 // Need to use compare because subtraction will wrap
50 // to positive for very large neg numbers, etc.
51 if (m_value
< value) return -1;
52 if (m_value
> value) return 1;
56 public override bool Equals(object? obj
)
62 return m_value
== ((long)obj
).m_value
;
66 public bool Equals(long obj
)
68 return m_value
== obj
;
71 // The value of the lower 32 bits XORed with the uppper 32 bits.
72 public override int GetHashCode()
74 return unchecked((int)((long)m_value
)) ^
(int)(m_value
>> 32);
77 public override string ToString()
79 return Number
.FormatInt64(m_value
, null, null);
82 public string ToString(IFormatProvider
? provider
)
84 return Number
.FormatInt64(m_value
, null, provider
);
87 public string ToString(string? format
)
89 return Number
.FormatInt64(m_value
, format
, null);
92 public string ToString(string? format
, IFormatProvider
? provider
)
94 return Number
.FormatInt64(m_value
, format
, provider
);
97 public bool TryFormat(Span
<char> destination
, out int charsWritten
, ReadOnlySpan
<char> format
= default, IFormatProvider
? provider
= null)
99 return Number
.TryFormatInt64(m_value
, format
, provider
, destination
, out charsWritten
);
102 public static long Parse(string s
)
104 if (s
== null) ThrowHelper
.ThrowArgumentNullException(ExceptionArgument
.s
);
105 return Number
.ParseInt64(s
, NumberStyles
.Integer
, NumberFormatInfo
.CurrentInfo
);
108 public static long Parse(string s
, NumberStyles style
)
110 NumberFormatInfo
.ValidateParseStyleInteger(style
);
111 if (s
== null) ThrowHelper
.ThrowArgumentNullException(ExceptionArgument
.s
);
112 return Number
.ParseInt64(s
, style
, NumberFormatInfo
.CurrentInfo
);
115 public static long Parse(string s
, IFormatProvider
? provider
)
117 if (s
== null) ThrowHelper
.ThrowArgumentNullException(ExceptionArgument
.s
);
118 return Number
.ParseInt64(s
, NumberStyles
.Integer
, NumberFormatInfo
.GetInstance(provider
));
121 // Parses a long from a String in the given style. If
122 // a NumberFormatInfo isn't specified, the current culture's
123 // NumberFormatInfo is assumed.
125 public static long Parse(string s
, NumberStyles style
, IFormatProvider
? provider
)
127 NumberFormatInfo
.ValidateParseStyleInteger(style
);
128 if (s
== null) ThrowHelper
.ThrowArgumentNullException(ExceptionArgument
.s
);
129 return Number
.ParseInt64(s
, style
, NumberFormatInfo
.GetInstance(provider
));
132 public static long Parse(ReadOnlySpan
<char> s
, NumberStyles style
= NumberStyles
.Integer
, IFormatProvider
? provider
= null)
134 NumberFormatInfo
.ValidateParseStyleInteger(style
);
135 return Number
.ParseInt64(s
, style
, NumberFormatInfo
.GetInstance(provider
));
138 public static bool TryParse(string? s
, out long result
)
146 return Number
.TryParseInt64IntegerStyle(s
, NumberStyles
.Integer
, NumberFormatInfo
.CurrentInfo
, out result
) == Number
.ParsingStatus
.OK
;
149 public static bool TryParse(ReadOnlySpan
<char> s
, out long result
)
151 return Number
.TryParseInt64IntegerStyle(s
, NumberStyles
.Integer
, NumberFormatInfo
.CurrentInfo
, out result
) == Number
.ParsingStatus
.OK
;
154 public static bool TryParse(string? s
, NumberStyles style
, IFormatProvider
? provider
, out long result
)
156 NumberFormatInfo
.ValidateParseStyleInteger(style
);
164 return Number
.TryParseInt64(s
, style
, NumberFormatInfo
.GetInstance(provider
), out result
) == Number
.ParsingStatus
.OK
;
167 public static bool TryParse(ReadOnlySpan
<char> s
, NumberStyles style
, IFormatProvider
? provider
, out long result
)
169 NumberFormatInfo
.ValidateParseStyleInteger(style
);
170 return Number
.TryParseInt64(s
, style
, NumberFormatInfo
.GetInstance(provider
), out result
) == Number
.ParsingStatus
.OK
;
174 // IConvertible implementation
177 public TypeCode
GetTypeCode()
179 return TypeCode
.Int64
;
182 bool IConvertible
.ToBoolean(IFormatProvider
? provider
)
184 return Convert
.ToBoolean(m_value
);
187 char IConvertible
.ToChar(IFormatProvider
? provider
)
189 return Convert
.ToChar(m_value
);
192 sbyte IConvertible
.ToSByte(IFormatProvider
? provider
)
194 return Convert
.ToSByte(m_value
);
197 byte IConvertible
.ToByte(IFormatProvider
? provider
)
199 return Convert
.ToByte(m_value
);
202 short IConvertible
.ToInt16(IFormatProvider
? provider
)
204 return Convert
.ToInt16(m_value
);
207 ushort IConvertible
.ToUInt16(IFormatProvider
? provider
)
209 return Convert
.ToUInt16(m_value
);
212 int IConvertible
.ToInt32(IFormatProvider
? provider
)
214 return Convert
.ToInt32(m_value
);
217 uint IConvertible
.ToUInt32(IFormatProvider
? provider
)
219 return Convert
.ToUInt32(m_value
);
222 long IConvertible
.ToInt64(IFormatProvider
? provider
)
227 ulong IConvertible
.ToUInt64(IFormatProvider
? provider
)
229 return Convert
.ToUInt64(m_value
);
232 float IConvertible
.ToSingle(IFormatProvider
? provider
)
234 return Convert
.ToSingle(m_value
);
237 double IConvertible
.ToDouble(IFormatProvider
? provider
)
239 return Convert
.ToDouble(m_value
);
242 decimal IConvertible
.ToDecimal(IFormatProvider
? provider
)
244 return Convert
.ToDecimal(m_value
);
247 DateTime IConvertible
.ToDateTime(IFormatProvider
? provider
)
249 throw new InvalidCastException(SR
.Format(SR
.InvalidCast_FromTo
, "Int64", "DateTime"));
252 object IConvertible
.ToType(Type type
, IFormatProvider
? provider
)
254 return Convert
.DefaultToType((IConvertible
)this, type
, provider
);