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 
5 using System.Globalization;
6 using System.Runtime.CompilerServices;
7 using System.Runtime.InteropServices;
8 using System.Runtime.Versioning;
9 
10 namespace System
11 {
12     [Serializable]
13     [StructLayout(LayoutKind.Sequential)]
14     [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
15     public struct Int64 : IComparable, IConvertible, IFormattable, IComparable<Int64>, IEquatable<Int64>, ISpanFormattable
16     {
17         private long m_value; // Do not rename (binary serialization)
18 
19         public const long MaxValue = 0x7fffffffffffffffL;
20         public const long MinValue = unchecked((long)0x8000000000000000L);
21 
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.
27         //
CompareToSystem.Int6428         public int CompareTo(Object value)
29         {
30             if (value == null)
31             {
32                 return 1;
33             }
34             if (value is Int64)
35             {
36                 // Need to use compare because subtraction will wrap
37                 // to positive for very large neg numbers, etc.
38                 long i = (long)value;
39                 if (m_value < i) return -1;
40                 if (m_value > i) return 1;
41                 return 0;
42             }
43             throw new ArgumentException(SR.Arg_MustBeInt64);
44         }
45 
CompareToSystem.Int6446         public int CompareTo(Int64 value)
47         {
48             // Need to use compare because subtraction will wrap
49             // to positive for very large neg numbers, etc.
50             if (m_value < value) return -1;
51             if (m_value > value) return 1;
52             return 0;
53         }
54 
EqualsSystem.Int6455         public override bool Equals(Object obj)
56         {
57             if (!(obj is Int64))
58             {
59                 return false;
60             }
61             return m_value == ((Int64)obj).m_value;
62         }
63 
64         [NonVersionable]
EqualsSystem.Int6465         public bool Equals(Int64 obj)
66         {
67             return m_value == obj;
68         }
69 
70         // The value of the lower 32 bits XORed with the uppper 32 bits.
GetHashCodeSystem.Int6471         public override int GetHashCode()
72         {
73             return (unchecked((int)((long)m_value)) ^ (int)(m_value >> 32));
74         }
75 
ToStringSystem.Int6476         public override String ToString()
77         {
78             return Number.FormatInt64(m_value, null, null);
79         }
80 
ToStringSystem.Int6481         public String ToString(IFormatProvider provider)
82         {
83             return Number.FormatInt64(m_value, null, provider);
84         }
85 
ToStringSystem.Int6486         public String ToString(String format)
87         {
88             return Number.FormatInt64(m_value, format, null);
89         }
90 
ToStringSystem.Int6491         public String ToString(String format, IFormatProvider provider)
92         {
93             return Number.FormatInt64(m_value, format, provider);
94         }
95 
TryFormatSystem.Int6496         public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
97         {
98             return Number.TryFormatInt64(m_value, format, provider, destination, out charsWritten);
99         }
100 
ParseSystem.Int64101         public static long Parse(String s)
102         {
103             if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
104             return Number.ParseInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
105         }
106 
ParseSystem.Int64107         public static long Parse(String s, NumberStyles style)
108         {
109             NumberFormatInfo.ValidateParseStyleInteger(style);
110             if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
111             return Number.ParseInt64(s, style, NumberFormatInfo.CurrentInfo);
112         }
113 
ParseSystem.Int64114         public static long Parse(String s, IFormatProvider provider)
115         {
116             if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
117             return Number.ParseInt64(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
118         }
119 
120 
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.
124         //
ParseSystem.Int64125         public static long Parse(String s, NumberStyles style, IFormatProvider provider)
126         {
127             NumberFormatInfo.ValidateParseStyleInteger(style);
128             if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
129             return Number.ParseInt64(s, style, NumberFormatInfo.GetInstance(provider));
130         }
131 
ParseSystem.Int64132         public static long Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
133         {
134             NumberFormatInfo.ValidateParseStyleInteger(style);
135             return Number.ParseInt64(s, style, NumberFormatInfo.GetInstance(provider));
136         }
137 
TryParseSystem.Int64138         public static Boolean TryParse(String s, out Int64 result)
139         {
140             if (s == null)
141             {
142                 result = 0;
143                 return false;
144             }
145 
146             return Number.TryParseInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
147         }
148 
TryParseSystem.Int64149         public static bool TryParse(ReadOnlySpan<char> s, out long result)
150         {
151             return Number.TryParseInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
152         }
153 
TryParseSystem.Int64154         public static Boolean TryParse(String s, NumberStyles style, IFormatProvider provider, out Int64 result)
155         {
156             NumberFormatInfo.ValidateParseStyleInteger(style);
157 
158             if (s == null)
159             {
160                 result = 0;
161                 return false;
162             }
163 
164             return Number.TryParseInt64(s, style, NumberFormatInfo.GetInstance(provider), out result);
165         }
166 
TryParseSystem.Int64167         public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out long result)
168         {
169             NumberFormatInfo.ValidateParseStyleInteger(style);
170             return Number.TryParseInt64(s, style, NumberFormatInfo.GetInstance(provider), out result);
171         }
172 
173         //
174         // IConvertible implementation
175         //
176 
GetTypeCodeSystem.Int64177         public TypeCode GetTypeCode()
178         {
179             return TypeCode.Int64;
180         }
181 
IConvertible.ToBooleanSystem.Int64182         bool IConvertible.ToBoolean(IFormatProvider provider)
183         {
184             return Convert.ToBoolean(m_value);
185         }
186 
IConvertible.ToCharSystem.Int64187         char IConvertible.ToChar(IFormatProvider provider)
188         {
189             return Convert.ToChar(m_value);
190         }
191 
IConvertible.ToSByteSystem.Int64192         sbyte IConvertible.ToSByte(IFormatProvider provider)
193         {
194             return Convert.ToSByte(m_value);
195         }
196 
IConvertible.ToByteSystem.Int64197         byte IConvertible.ToByte(IFormatProvider provider)
198         {
199             return Convert.ToByte(m_value);
200         }
201 
IConvertible.ToInt16System.Int64202         short IConvertible.ToInt16(IFormatProvider provider)
203         {
204             return Convert.ToInt16(m_value);
205         }
206 
IConvertible.ToUInt16System.Int64207         ushort IConvertible.ToUInt16(IFormatProvider provider)
208         {
209             return Convert.ToUInt16(m_value);
210         }
211 
IConvertible.ToInt32System.Int64212         int IConvertible.ToInt32(IFormatProvider provider)
213         {
214             return Convert.ToInt32(m_value);
215         }
216 
IConvertible.ToUInt32System.Int64217         uint IConvertible.ToUInt32(IFormatProvider provider)
218         {
219             return Convert.ToUInt32(m_value);
220         }
221 
IConvertible.ToInt64System.Int64222         long IConvertible.ToInt64(IFormatProvider provider)
223         {
224             return m_value;
225         }
226 
IConvertible.ToUInt64System.Int64227         ulong IConvertible.ToUInt64(IFormatProvider provider)
228         {
229             return Convert.ToUInt64(m_value);
230         }
231 
IConvertible.ToSingleSystem.Int64232         float IConvertible.ToSingle(IFormatProvider provider)
233         {
234             return Convert.ToSingle(m_value);
235         }
236 
IConvertible.ToDoubleSystem.Int64237         double IConvertible.ToDouble(IFormatProvider provider)
238         {
239             return Convert.ToDouble(m_value);
240         }
241 
IConvertible.ToDecimalSystem.Int64242         Decimal IConvertible.ToDecimal(IFormatProvider provider)
243         {
244             return Convert.ToDecimal(m_value);
245         }
246 
IConvertible.ToDateTimeSystem.Int64247         DateTime IConvertible.ToDateTime(IFormatProvider provider)
248         {
249             throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Int64", "DateTime"));
250         }
251 
IConvertible.ToTypeSystem.Int64252         Object IConvertible.ToType(Type type, IFormatProvider provider)
253         {
254             return Convert.DefaultToType((IConvertible)this, type, provider);
255         }
256     }
257 }
258