Byte.cs 8.8 KB

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