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 Int32
: IComparable
, IConvertible
, IFormattable
, IComparable
<int>, IEquatable
<int>, ISpanFormattable
17 private readonly int m_value
; // Do not rename (binary serialization)
19 public const int MaxValue
= 0x7fffffff;
20 public const int MinValue
= unchecked((int)0x80000000);
22 // Compares this object to another object, returning an integer that
23 // indicates the relationship.
25 // 0 if the values are equal
26 // Negative number if _value is less than value
27 // Positive number if _value is more than value
28 // null is considered to be less than any instance, hence returns positive number
29 // If object is not of type Int32, this method throws an ArgumentException.
31 public int CompareTo(object? value)
39 // NOTE: Cannot use return (_value - value) as this causes a wrap
40 // around in cases where _value - value > MaxValue.
42 if (m_value
< i
) return -1;
43 if (m_value
> i
) return 1;
46 throw new ArgumentException(SR
.Arg_MustBeInt32
);
49 public int CompareTo(int value)
51 // NOTE: Cannot use return (_value - value) as this causes a wrap
52 // around in cases where _value - value > MaxValue.
53 if (m_value
< value) return -1;
54 if (m_value
> value) return 1;
58 public override bool Equals(object? obj
)
64 return m_value
== ((int)obj
).m_value
;
68 public bool Equals(int obj
)
70 return m_value
== obj
;
73 // The absolute value of the int contained.
74 public override int GetHashCode()
79 public override string ToString()
81 return Number
.FormatInt32(m_value
, null, null);
84 public string ToString(string? format
)
86 return Number
.FormatInt32(m_value
, format
, null);
89 public string ToString(IFormatProvider
? provider
)
91 return Number
.FormatInt32(m_value
, null, provider
);
94 public string ToString(string? format
, IFormatProvider
? provider
)
96 return Number
.FormatInt32(m_value
, format
, provider
);
99 public bool TryFormat(Span
<char> destination
, out int charsWritten
, ReadOnlySpan
<char> format
= default, IFormatProvider
? provider
= null)
101 return Number
.TryFormatInt32(m_value
, format
, provider
, destination
, out charsWritten
);
104 public static int Parse(string s
)
106 if (s
== null) ThrowHelper
.ThrowArgumentNullException(ExceptionArgument
.s
);
107 return Number
.ParseInt32(s
, NumberStyles
.Integer
, NumberFormatInfo
.CurrentInfo
);
110 public static int Parse(string s
, NumberStyles style
)
112 NumberFormatInfo
.ValidateParseStyleInteger(style
);
113 if (s
== null) ThrowHelper
.ThrowArgumentNullException(ExceptionArgument
.s
);
114 return Number
.ParseInt32(s
, style
, NumberFormatInfo
.CurrentInfo
);
117 // Parses an integer from a String in the given style. If
118 // a NumberFormatInfo isn't specified, the current culture's
119 // NumberFormatInfo is assumed.
121 public static int Parse(string s
, IFormatProvider
? provider
)
123 if (s
== null) ThrowHelper
.ThrowArgumentNullException(ExceptionArgument
.s
);
124 return Number
.ParseInt32(s
, NumberStyles
.Integer
, NumberFormatInfo
.GetInstance(provider
));
127 // Parses an integer from a String in the given style. If
128 // a NumberFormatInfo isn't specified, the current culture's
129 // NumberFormatInfo is assumed.
131 public static int Parse(string s
, NumberStyles style
, IFormatProvider
? provider
)
133 NumberFormatInfo
.ValidateParseStyleInteger(style
);
134 if (s
== null) ThrowHelper
.ThrowArgumentNullException(ExceptionArgument
.s
);
135 return Number
.ParseInt32(s
, style
, NumberFormatInfo
.GetInstance(provider
));
138 public static int Parse(ReadOnlySpan
<char> s
, NumberStyles style
= NumberStyles
.Integer
, IFormatProvider
? provider
= null)
140 NumberFormatInfo
.ValidateParseStyleInteger(style
);
141 return Number
.ParseInt32(s
, style
, NumberFormatInfo
.GetInstance(provider
));
144 // Parses an integer from a String. Returns false rather
145 // than throwing exceptin if input is invalid
147 public static bool TryParse(string? s
, out int result
)
155 return Number
.TryParseInt32IntegerStyle(s
, NumberStyles
.Integer
, NumberFormatInfo
.CurrentInfo
, out result
) == Number
.ParsingStatus
.OK
;
158 public static bool TryParse(ReadOnlySpan
<char> s
, out int result
)
160 return Number
.TryParseInt32IntegerStyle(s
, NumberStyles
.Integer
, NumberFormatInfo
.CurrentInfo
, out result
) == Number
.ParsingStatus
.OK
;
163 // Parses an integer from a String in the given style. Returns false rather
164 // than throwing exceptin if input is invalid
166 public static bool TryParse(string? s
, NumberStyles style
, IFormatProvider
? provider
, out int result
)
168 NumberFormatInfo
.ValidateParseStyleInteger(style
);
176 return Number
.TryParseInt32(s
, style
, NumberFormatInfo
.GetInstance(provider
), out result
) == Number
.ParsingStatus
.OK
;
179 public static bool TryParse(ReadOnlySpan
<char> s
, NumberStyles style
, IFormatProvider
? provider
, out int result
)
181 NumberFormatInfo
.ValidateParseStyleInteger(style
);
182 return Number
.TryParseInt32(s
, style
, NumberFormatInfo
.GetInstance(provider
), out result
) == Number
.ParsingStatus
.OK
;
186 // IConvertible implementation
189 public TypeCode
GetTypeCode()
191 return TypeCode
.Int32
;
194 bool IConvertible
.ToBoolean(IFormatProvider
? provider
)
196 return Convert
.ToBoolean(m_value
);
199 char IConvertible
.ToChar(IFormatProvider
? provider
)
201 return Convert
.ToChar(m_value
);
204 sbyte IConvertible
.ToSByte(IFormatProvider
? provider
)
206 return Convert
.ToSByte(m_value
);
209 byte IConvertible
.ToByte(IFormatProvider
? provider
)
211 return Convert
.ToByte(m_value
);
214 short IConvertible
.ToInt16(IFormatProvider
? provider
)
216 return Convert
.ToInt16(m_value
);
219 ushort IConvertible
.ToUInt16(IFormatProvider
? provider
)
221 return Convert
.ToUInt16(m_value
);
224 int IConvertible
.ToInt32(IFormatProvider
? provider
)
229 uint IConvertible
.ToUInt32(IFormatProvider
? provider
)
231 return Convert
.ToUInt32(m_value
);
234 long IConvertible
.ToInt64(IFormatProvider
? provider
)
236 return Convert
.ToInt64(m_value
);
239 ulong IConvertible
.ToUInt64(IFormatProvider
? provider
)
241 return Convert
.ToUInt64(m_value
);
244 float IConvertible
.ToSingle(IFormatProvider
? provider
)
246 return Convert
.ToSingle(m_value
);
249 double IConvertible
.ToDouble(IFormatProvider
? provider
)
251 return Convert
.ToDouble(m_value
);
254 decimal IConvertible
.ToDecimal(IFormatProvider
? provider
)
256 return Convert
.ToDecimal(m_value
);
259 DateTime IConvertible
.ToDateTime(IFormatProvider
? provider
)
261 throw new InvalidCastException(SR
.Format(SR
.InvalidCast_FromTo
, "Int32", "DateTime"));
264 object IConvertible
.ToType(Type type
, IFormatProvider
? provider
)
266 return Convert
.DefaultToType((IConvertible
)this, type
, provider
);