Fix pragma warning restore (dotnet/coreclr#26389)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / UInt64.cs
blob2999ba87ecab1dd0b851db6dda3fa54e88423ef0
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 [CLSCompliant(false)]
14 [StructLayout(LayoutKind.Sequential)]
15 [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
16 public readonly struct UInt64 : IComparable, IConvertible, IFormattable, IComparable<ulong>, IEquatable<ulong>, ISpanFormattable
18 private readonly ulong m_value; // Do not rename (binary serialization)
20 public const ulong MaxValue = (ulong)0xffffffffffffffffL;
21 public const ulong MinValue = 0x0;
23 // Compares this object to another object, returning an integer that
24 // indicates the relationship.
25 // Returns a value less than zero if this object
26 // null is considered to be less than any instance.
27 // If object is not of type UInt64, this method throws an ArgumentException.
29 public int CompareTo(object? value)
31 if (value == null)
33 return 1;
36 // Need to use compare because subtraction will wrap
37 // to positive for very large neg numbers, etc.
38 if (value is ulong i)
40 if (m_value < i) return -1;
41 if (m_value > i) return 1;
42 return 0;
45 throw new ArgumentException(SR.Arg_MustBeUInt64);
48 public int CompareTo(ulong value)
50 // Need to use compare because subtraction will wrap
51 // to positive for very large neg numbers, etc.
52 if (m_value < value) return -1;
53 if (m_value > value) return 1;
54 return 0;
57 public override bool Equals(object? obj)
59 if (!(obj is ulong))
61 return false;
63 return m_value == ((ulong)obj).m_value;
66 [NonVersionable]
67 public bool Equals(ulong obj)
69 return m_value == obj;
72 // The value of the lower 32 bits XORed with the uppper 32 bits.
73 public override int GetHashCode()
75 return ((int)m_value) ^ (int)(m_value >> 32);
78 public override string ToString()
80 return Number.FormatUInt64(m_value, null, null);
83 public string ToString(IFormatProvider? provider)
85 return Number.FormatUInt64(m_value, null, provider);
88 public string ToString(string? format)
90 return Number.FormatUInt64(m_value, format, null);
93 public string ToString(string? format, IFormatProvider? provider)
95 return Number.FormatUInt64(m_value, format, provider);
98 public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
100 return Number.TryFormatUInt64(m_value, format, provider, destination, out charsWritten);
103 [CLSCompliant(false)]
104 public static ulong Parse(string s)
106 if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
107 return Number.ParseUInt64(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
110 [CLSCompliant(false)]
111 public static ulong Parse(string s, NumberStyles style)
113 NumberFormatInfo.ValidateParseStyleInteger(style);
114 if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
115 return Number.ParseUInt64(s, style, NumberFormatInfo.CurrentInfo);
118 [CLSCompliant(false)]
119 public static ulong Parse(string s, IFormatProvider? provider)
121 if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
122 return Number.ParseUInt64(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
125 [CLSCompliant(false)]
126 public static ulong Parse(string s, NumberStyles style, IFormatProvider? provider)
128 NumberFormatInfo.ValidateParseStyleInteger(style);
129 if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
130 return Number.ParseUInt64(s, style, NumberFormatInfo.GetInstance(provider));
133 [CLSCompliant(false)]
134 public static ulong Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider? provider = null)
136 NumberFormatInfo.ValidateParseStyleInteger(style);
137 return Number.ParseUInt64(s, style, NumberFormatInfo.GetInstance(provider));
140 [CLSCompliant(false)]
141 public static bool TryParse(string? s, out ulong result)
143 if (s == null)
145 result = 0;
146 return false;
149 return Number.TryParseUInt64IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
152 [CLSCompliant(false)]
153 public static bool TryParse(ReadOnlySpan<char> s, out ulong result)
155 return Number.TryParseUInt64IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
158 [CLSCompliant(false)]
159 public static bool TryParse(string? s, NumberStyles style, IFormatProvider? provider, out ulong result)
161 NumberFormatInfo.ValidateParseStyleInteger(style);
163 if (s == null)
165 result = 0;
166 return false;
169 return Number.TryParseUInt64(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
172 [CLSCompliant(false)]
173 public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out ulong result)
175 NumberFormatInfo.ValidateParseStyleInteger(style);
176 return Number.TryParseUInt64(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
180 // IConvertible implementation
183 public TypeCode GetTypeCode()
185 return TypeCode.UInt64;
188 bool IConvertible.ToBoolean(IFormatProvider? provider)
190 return Convert.ToBoolean(m_value);
193 char IConvertible.ToChar(IFormatProvider? provider)
195 return Convert.ToChar(m_value);
198 sbyte IConvertible.ToSByte(IFormatProvider? provider)
200 return Convert.ToSByte(m_value);
203 byte IConvertible.ToByte(IFormatProvider? provider)
205 return Convert.ToByte(m_value);
208 short IConvertible.ToInt16(IFormatProvider? provider)
210 return Convert.ToInt16(m_value);
213 ushort IConvertible.ToUInt16(IFormatProvider? provider)
215 return Convert.ToUInt16(m_value);
218 int IConvertible.ToInt32(IFormatProvider? provider)
220 return Convert.ToInt32(m_value);
223 uint IConvertible.ToUInt32(IFormatProvider? provider)
225 return Convert.ToUInt32(m_value);
228 long IConvertible.ToInt64(IFormatProvider? provider)
230 return Convert.ToInt64(m_value);
233 ulong IConvertible.ToUInt64(IFormatProvider? provider)
235 return m_value;
238 float IConvertible.ToSingle(IFormatProvider? provider)
240 return Convert.ToSingle(m_value);
243 double IConvertible.ToDouble(IFormatProvider? provider)
245 return Convert.ToDouble(m_value);
248 decimal IConvertible.ToDecimal(IFormatProvider? provider)
250 return Convert.ToDecimal(m_value);
253 DateTime IConvertible.ToDateTime(IFormatProvider? provider)
255 throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "UInt64", "DateTime"));
258 object IConvertible.ToType(Type type, IFormatProvider? provider)
260 return Convert.DefaultToType((IConvertible)this, type, provider);