UInt16.cs 8.9 KB

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