Int32.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. if (value is int)
  34. {
  35. // NOTE: Cannot use return (_value - value) as this causes a wrap
  36. // around in cases where _value - value > MaxValue.
  37. int i = (int)value;
  38. if (m_value < i) return -1;
  39. if (m_value > i) return 1;
  40. return 0;
  41. }
  42. throw new ArgumentException(SR.Arg_MustBeInt32);
  43. }
  44. public int CompareTo(int value)
  45. {
  46. // NOTE: Cannot use return (_value - value) as this causes a wrap
  47. // around in cases where _value - value > MaxValue.
  48. if (m_value < value) return -1;
  49. if (m_value > value) return 1;
  50. return 0;
  51. }
  52. public override bool Equals(object obj)
  53. {
  54. if (!(obj is int))
  55. {
  56. return false;
  57. }
  58. return m_value == ((int)obj).m_value;
  59. }
  60. [NonVersionable]
  61. public bool Equals(int obj)
  62. {
  63. return m_value == obj;
  64. }
  65. // The absolute value of the int contained.
  66. public override int GetHashCode()
  67. {
  68. return m_value;
  69. }
  70. public override string ToString()
  71. {
  72. return Number.FormatInt32(m_value, null, null);
  73. }
  74. public string ToString(string format)
  75. {
  76. return Number.FormatInt32(m_value, format, null);
  77. }
  78. public string ToString(IFormatProvider provider)
  79. {
  80. return Number.FormatInt32(m_value, null, provider);
  81. }
  82. public string ToString(string format, IFormatProvider provider)
  83. {
  84. return Number.FormatInt32(m_value, format, provider);
  85. }
  86. public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider provider = null)
  87. {
  88. return Number.TryFormatInt32(m_value, format, provider, destination, out charsWritten);
  89. }
  90. public static int Parse(string s)
  91. {
  92. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  93. return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
  94. }
  95. public static int Parse(string s, NumberStyles style)
  96. {
  97. NumberFormatInfo.ValidateParseStyleInteger(style);
  98. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  99. return Number.ParseInt32(s, style, NumberFormatInfo.CurrentInfo);
  100. }
  101. // Parses an integer from a String in the given style. If
  102. // a NumberFormatInfo isn't specified, the current culture's
  103. // NumberFormatInfo is assumed.
  104. //
  105. public static int Parse(string s, IFormatProvider provider)
  106. {
  107. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  108. return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
  109. }
  110. // Parses an integer from a String in the given style. If
  111. // a NumberFormatInfo isn't specified, the current culture's
  112. // NumberFormatInfo is assumed.
  113. //
  114. public static int Parse(string s, NumberStyles style, IFormatProvider provider)
  115. {
  116. NumberFormatInfo.ValidateParseStyleInteger(style);
  117. if (s == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s);
  118. return Number.ParseInt32(s, style, NumberFormatInfo.GetInstance(provider));
  119. }
  120. public static int Parse(ReadOnlySpan<char> s, NumberStyles style = NumberStyles.Integer, IFormatProvider provider = null)
  121. {
  122. NumberFormatInfo.ValidateParseStyleInteger(style);
  123. return Number.ParseInt32(s, style, NumberFormatInfo.GetInstance(provider));
  124. }
  125. // Parses an integer from a String. Returns false rather
  126. // than throwing exceptin if input is invalid
  127. //
  128. public static bool TryParse(string s, out int result)
  129. {
  130. if (s == null)
  131. {
  132. result = 0;
  133. return false;
  134. }
  135. return Number.TryParseInt32IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
  136. }
  137. public static bool TryParse(ReadOnlySpan<char> s, out int result)
  138. {
  139. return Number.TryParseInt32IntegerStyle(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result) == Number.ParsingStatus.OK;
  140. }
  141. // Parses an integer from a String in the given style. Returns false rather
  142. // than throwing exceptin if input is invalid
  143. //
  144. public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out int result)
  145. {
  146. NumberFormatInfo.ValidateParseStyleInteger(style);
  147. if (s == null)
  148. {
  149. result = 0;
  150. return false;
  151. }
  152. return Number.TryParseInt32(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
  153. }
  154. public static bool TryParse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider provider, out int result)
  155. {
  156. NumberFormatInfo.ValidateParseStyleInteger(style);
  157. return Number.TryParseInt32(s, style, NumberFormatInfo.GetInstance(provider), out result) == Number.ParsingStatus.OK;
  158. }
  159. //
  160. // IConvertible implementation
  161. //
  162. public TypeCode GetTypeCode()
  163. {
  164. return TypeCode.Int32;
  165. }
  166. bool IConvertible.ToBoolean(IFormatProvider provider)
  167. {
  168. return Convert.ToBoolean(m_value);
  169. }
  170. char IConvertible.ToChar(IFormatProvider provider)
  171. {
  172. return Convert.ToChar(m_value);
  173. }
  174. sbyte IConvertible.ToSByte(IFormatProvider provider)
  175. {
  176. return Convert.ToSByte(m_value);
  177. }
  178. byte IConvertible.ToByte(IFormatProvider provider)
  179. {
  180. return Convert.ToByte(m_value);
  181. }
  182. short IConvertible.ToInt16(IFormatProvider provider)
  183. {
  184. return Convert.ToInt16(m_value);
  185. }
  186. ushort IConvertible.ToUInt16(IFormatProvider provider)
  187. {
  188. return Convert.ToUInt16(m_value);
  189. }
  190. int IConvertible.ToInt32(IFormatProvider provider)
  191. {
  192. return m_value;
  193. }
  194. uint IConvertible.ToUInt32(IFormatProvider provider)
  195. {
  196. return Convert.ToUInt32(m_value);
  197. }
  198. long IConvertible.ToInt64(IFormatProvider provider)
  199. {
  200. return Convert.ToInt64(m_value);
  201. }
  202. ulong IConvertible.ToUInt64(IFormatProvider provider)
  203. {
  204. return Convert.ToUInt64(m_value);
  205. }
  206. float IConvertible.ToSingle(IFormatProvider provider)
  207. {
  208. return Convert.ToSingle(m_value);
  209. }
  210. double IConvertible.ToDouble(IFormatProvider provider)
  211. {
  212. return Convert.ToDouble(m_value);
  213. }
  214. decimal IConvertible.ToDecimal(IFormatProvider provider)
  215. {
  216. return Convert.ToDecimal(m_value);
  217. }
  218. DateTime IConvertible.ToDateTime(IFormatProvider provider)
  219. {
  220. throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Int32", "DateTime"));
  221. }
  222. object IConvertible.ToType(Type type, IFormatProvider provider)
  223. {
  224. return Convert.DefaultToType((IConvertible)this, type, provider);
  225. }
  226. }
  227. }