Int32.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. using System.Globalization;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. using System.Runtime.Versioning;
  8. namespace System
  9. {
  10. [Serializable]
  11. [StructLayout(LayoutKind.Sequential)]
  12. [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  13. public readonly struct Int32 : IComparable, IConvertible, IFormattable, IComparable<int>, IEquatable<int>, ISpanFormattable
  14. {
  15. private readonly int m_value; // Do not rename (binary serialization)
  16. public const int MaxValue = 0x7fffffff;
  17. public const int MinValue = unchecked((int)0x80000000);
  18. // Compares this object to another object, returning an integer that
  19. // indicates the relationship.
  20. // Returns :
  21. // 0 if the values are equal
  22. // Negative number if _value is less than value
  23. // Positive number if _value is more than value
  24. // null is considered to be less than any instance, hence returns positive number
  25. // If object is not of type Int32, this method throws an ArgumentException.
  26. //
  27. public int CompareTo(object? value)
  28. {
  29. if (value == null)
  30. {
  31. return 1;
  32. }
  33. // NOTE: Cannot use return (_value - value) as this causes a wrap
  34. // around in cases where _value - value > MaxValue.
  35. if (value is int i)
  36. {
  37. if (m_value < i) return -1;
  38. if (m_value > i) return 1;
  39. return 0;
  40. }
  41. throw new ArgumentException(SR.Arg_MustBeInt32);
  42. }
  43. public int CompareTo(int value)
  44. {
  45. // NOTE: Cannot use return (_value - value) as this causes a wrap
  46. // around in cases where _value - value > MaxValue.
  47. if (m_value < value) return -1;
  48. if (m_value > value) return 1;
  49. return 0;
  50. }
  51. public override bool Equals(object? obj)
  52. {
  53. if (!(obj is int))
  54. {
  55. return false;
  56. }
  57. return m_value == ((int)obj).m_value;
  58. }
  59. [NonVersionable]
  60. public bool Equals(int obj)
  61. {
  62. return m_value == obj;
  63. }
  64. // The absolute value of the int contained.
  65. public override int GetHashCode()
  66. {
  67. return m_value;
  68. }
  69. public override string ToString()
  70. {
  71. return Number.FormatInt32(m_value, null, null);
  72. }
  73. public string ToString(string? format)
  74. {
  75. return Number.FormatInt32(m_value, format, null);
  76. }
  77. public string ToString(IFormatProvider? provider)
  78. {
  79. return Number.FormatInt32(m_value, null, provider);
  80. }
  81. public string ToString(string? format, IFormatProvider? provider)
  82. {
  83. return Number.FormatInt32(m_value, format, provider);
  84. }
  85. public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
  86. {
  87. return Number.TryFormatInt32(m_value, format, provider, destination, out charsWritten);
  88. }
  89. public static int Parse(string s)
  90. {
  91. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  92. return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
  93. }
  94. public static int Parse(string s, NumberStyles style)
  95. {
  96. NumberFormatInfo.ValidateParseStyleInteger(style);
  97. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  98. return Number.ParseInt32(s, style, NumberFormatInfo.CurrentInfo);
  99. }
  100. // Parses an integer from a String in the given style. If
  101. // a NumberFormatInfo isn't specified, the current culture's
  102. // NumberFormatInfo is assumed.
  103. //
  104. public static int Parse(string s, IFormatProvider? provider)
  105. {
  106. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  107. return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
  108. }
  109. // Parses an integer from a String in the given style. If
  110. // a NumberFormatInfo isn't specified, the current culture's
  111. // NumberFormatInfo is assumed.
  112. //
  113. public static int Parse(string s, NumberStyles style, IFormatProvider? provider)
  114. {
  115. NumberFormatInfo.ValidateParseStyleInteger(style);
  116. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  117. return Number.ParseInt32(s, style, NumberFormatInfo.GetInstance(provider));
  118. }
  119. public static int Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider? provider = null)
  120. {
  121. NumberFormatInfo.ValidateParseStyleInteger(style);
  122. return Number.ParseInt32(s, style, NumberFormatInfo.GetInstance(provider));
  123. }
  124. // Parses an integer from a String. Returns false rather
  125. // than throwing exceptin if input is invalid
  126. //
  127. public static bool TryParse(string? s, out int result)
  128. {
  129. if (s == null)
  130. {
  131. result = 0;
  132. return false;
  133. }
  134. return Number.TryParseInt32IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
  135. }
  136. public static bool TryParse(ReadOnlySpan<char> s, out int result)
  137. {
  138. return Number.TryParseInt32IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
  139. }
  140. // Parses an integer from a String in the given style. Returns false rather
  141. // than throwing exceptin if input is invalid
  142. //
  143. public static bool TryParse(string? s, NumberStyles style, IFormatProvider? provider, out int result)
  144. {
  145. NumberFormatInfo.ValidateParseStyleInteger(style);
  146. if (s == null)
  147. {
  148. result = 0;
  149. return false;
  150. }
  151. return Number.TryParseInt32(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
  152. }
  153. public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider, out int result)
  154. {
  155. NumberFormatInfo.ValidateParseStyleInteger(style);
  156. return Number.TryParseInt32(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
  157. }
  158. //
  159. // IConvertible implementation
  160. //
  161. public TypeCode GetTypeCode()
  162. {
  163. return TypeCode.Int32;
  164. }
  165. bool IConvertible.ToBoolean(IFormatProvider? provider)
  166. {
  167. return Convert.ToBoolean(m_value);
  168. }
  169. char IConvertible.ToChar(IFormatProvider? provider)
  170. {
  171. return Convert.ToChar(m_value);
  172. }
  173. sbyte IConvertible.ToSByte(IFormatProvider? provider)
  174. {
  175. return Convert.ToSByte(m_value);
  176. }
  177. byte IConvertible.ToByte(IFormatProvider? provider)
  178. {
  179. return Convert.ToByte(m_value);
  180. }
  181. short IConvertible.ToInt16(IFormatProvider? provider)
  182. {
  183. return Convert.ToInt16(m_value);
  184. }
  185. ushort IConvertible.ToUInt16(IFormatProvider? provider)
  186. {
  187. return Convert.ToUInt16(m_value);
  188. }
  189. int IConvertible.ToInt32(IFormatProvider? provider)
  190. {
  191. return m_value;
  192. }
  193. uint IConvertible.ToUInt32(IFormatProvider? provider)
  194. {
  195. return Convert.ToUInt32(m_value);
  196. }
  197. long IConvertible.ToInt64(IFormatProvider? provider)
  198. {
  199. return Convert.ToInt64(m_value);
  200. }
  201. ulong IConvertible.ToUInt64(IFormatProvider? provider)
  202. {
  203. return Convert.ToUInt64(m_value);
  204. }
  205. float IConvertible.ToSingle(IFormatProvider? provider)
  206. {
  207. return Convert.ToSingle(m_value);
  208. }
  209. double IConvertible.ToDouble(IFormatProvider? provider)
  210. {
  211. return Convert.ToDouble(m_value);
  212. }
  213. decimal IConvertible.ToDecimal(IFormatProvider? provider)
  214. {
  215. return Convert.ToDecimal(m_value);
  216. }
  217. DateTime IConvertible.ToDateTime(IFormatProvider? provider)
  218. {
  219. throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Int32", "DateTime"));
  220. }
  221. object IConvertible.ToType(Type type, IFormatProvider? provider)
  222. {
  223. return Convert.DefaultToType((IConvertible)this, type, provider);
  224. }
  225. }
  226. }