Fix IDE0025 (use expression body for properties)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / Int64.cs
blob1084ba04e7ac564f723710ba8faee187cf21f499
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;
10 namespace System
12 [Serializable]
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)
30 if (value == null)
32 return 1;
35 // Need to use compare because subtraction will wrap
36 // to positive for very large neg numbers, etc.
37 if (value is long i)
39 if (m_value < i) return -1;
40 if (m_value > i) return 1;
41 return 0;
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;
53 return 0;
56 public override bool Equals(object? obj)
58 if (!(obj is long))
60 return false;
62 return m_value == ((long)obj).m_value;
65 [NonVersionable]
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));
122 // Parses a long from a String in the given style. If
123 // a NumberFormatInfo isn't specified, the current culture's
124 // NumberFormatInfo is assumed.
126 public static long Parse(string s, NumberStyles style, IFormatProvider? provider)
128 NumberFormatInfo.ValidateParseStyleInteger(style);
129 if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
130 return Number.ParseInt64(s, style, NumberFormatInfo.GetInstance(provider));
133 public static long Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider? provider = null)
135 NumberFormatInfo.ValidateParseStyleInteger(style);
136 return Number.ParseInt64(s, style, NumberFormatInfo.GetInstance(provider));
139 public static bool TryParse(string? s, out long result)
141 if (s == null)
143 result = 0;
144 return false;
147 return Number.TryParseInt64IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
150 public static bool TryParse(ReadOnlySpan<char> s, out long result)
152 return Number.TryParseInt64IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
155 public static bool TryParse(string? s, NumberStyles style, IFormatProvider? provider, out long result)
157 NumberFormatInfo.ValidateParseStyleInteger(style);
159 if (s == null)
161 result = 0;
162 return false;
165 return Number.TryParseInt64(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
168 public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out long result)
170 NumberFormatInfo.ValidateParseStyleInteger(style);
171 return Number.TryParseInt64(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
175 // IConvertible implementation
178 public TypeCode GetTypeCode()
180 return TypeCode.Int64;
183 bool IConvertible.ToBoolean(IFormatProvider? provider)
185 return Convert.ToBoolean(m_value);
188 char IConvertible.ToChar(IFormatProvider? provider)
190 return Convert.ToChar(m_value);
193 sbyte IConvertible.ToSByte(IFormatProvider? provider)
195 return Convert.ToSByte(m_value);
198 byte IConvertible.ToByte(IFormatProvider? provider)
200 return Convert.ToByte(m_value);
203 short IConvertible.ToInt16(IFormatProvider? provider)
205 return Convert.ToInt16(m_value);
208 ushort IConvertible.ToUInt16(IFormatProvider? provider)
210 return Convert.ToUInt16(m_value);
213 int IConvertible.ToInt32(IFormatProvider? provider)
215 return Convert.ToInt32(m_value);
218 uint IConvertible.ToUInt32(IFormatProvider? provider)
220 return Convert.ToUInt32(m_value);
223 long IConvertible.ToInt64(IFormatProvider? provider)
225 return m_value;
228 ulong IConvertible.ToUInt64(IFormatProvider? provider)
230 return Convert.ToUInt64(m_value);
233 float IConvertible.ToSingle(IFormatProvider? provider)
235 return Convert.ToSingle(m_value);
238 double IConvertible.ToDouble(IFormatProvider? provider)
240 return Convert.ToDouble(m_value);
243 decimal IConvertible.ToDecimal(IFormatProvider? provider)
245 return Convert.ToDecimal(m_value);
248 DateTime IConvertible.ToDateTime(IFormatProvider? provider)
250 throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Int64", "DateTime"));
253 object IConvertible.ToType(Type type, IFormatProvider? provider)
255 return Convert.DefaultToType((IConvertible)this, type, provider);