namespace System
{
[Serializable]
- public struct Byte : IFormattable, IConvertible,
-#if NET_2_0
- IComparable, IComparable<Byte>
-#else
- IComparable
-#endif
+ [System.Runtime.InteropServices.ComVisible (true)]
+ public struct Byte : IFormattable, IConvertible, IComparable, IComparable<Byte>, IEquatable <Byte>
{
public const byte MinValue = 0;
public const byte MaxValue = 255;
if (!(value is System.Byte))
throw new ArgumentException (Locale.GetText ("Value is not a System.Byte."));
- byte xv = (byte) value;
-
- if (m_value == xv)
- return 0;
- if (m_value > xv)
- return 1;
- else
- return -1;
+ return CompareTo ((byte) value);
}
public override bool Equals (object obj)
return m_value;
}
-#if NET_2_0
public int CompareTo (byte value)
{
- if (m_value == value)
- return 0;
- if (m_value > value)
- return 1;
- else
- return -1;
+ return m_value - value;
}
- public bool Equals (byte value)
+ public bool Equals (byte obj)
{
- return value == m_value;
- }
-#endif
-
- internal static bool Parse (string s, bool tryParse, out byte result)
- {
- byte val = 0;
- int len;
- int i;
- bool digits_seen = false;
- bool negative = false;
-
- result = 0;
-
- if (s == null)
- if (tryParse)
- return false;
- else
- throw new ArgumentNullException ("s");
-
- len = s.Length;
-
- // look for the first non-whitespace character
- char c;
- for (i = 0; i < len; i++){
- c = s [i];
- if (!Char.IsWhiteSpace (c))
- break;
- }
-
- // if it's all whitespace, then throw exception
- if (i == len)
- if (tryParse)
- return false;
- else
- throw new FormatException ();
-
- // look for the optional '+' sign
- if (s [i] == '+')
- i++;
- else if (s [i] == '-') {
- negative = true;
- i++;
- }
-
- // we should just have numerals followed by whitespace now
- for (; i < len; i++){
- c = s [i];
-
- if (c >= '0' && c <= '9'){
- // shift left and accumulate every time we find a numeral
- byte d = (byte) (c - '0');
-
- val = checked ((byte) (val * 10 + d));
- digits_seen = true;
- } else {
- // after the last numeral, only whitespace is allowed
- if (Char.IsWhiteSpace (c)){
- for (i++; i < len; i++){
- if (!Char.IsWhiteSpace (s [i]))
- if (tryParse)
- return false;
- else
- throw new FormatException ();
- }
- break;
- } else
- if (tryParse)
- return false;
- else
- throw new FormatException ();
- }
- }
-
- // -0 is legal but other negative values are not
- if (negative && (val > 0)) {
- if (tryParse)
- return false;
- else
- throw new OverflowException (
- Locale.GetText ("Negative number"));
- }
-
- // if all we had was a '+' sign, then throw exception
- if (!digits_seen)
- if (tryParse)
- return false;
- else
- throw new FormatException ();
-
- result = val;
- return true;
+ return m_value == obj;
}
public static byte Parse (string s, IFormatProvider provider)
public static byte Parse (string s, NumberStyles style, IFormatProvider provider)
{
uint tmpResult = UInt32.Parse (s, style, provider);
- if (tmpResult > Byte.MaxValue || tmpResult < Byte.MinValue)
- throw new OverflowException (Locale.GetText ("Value too large or too small."));
+ if (tmpResult > Byte.MaxValue)
+ throw new OverflowException (Locale.GetText ("Value too large."));
return (byte) tmpResult;
}
- public static byte Parse (string s) {
- byte res;
-
- Parse (s, false, out res);
-
- return res;
+ public static byte Parse (string s)
+ {
+ return Parse (s, NumberStyles.Integer, null);
}
-#if NET_2_0
- public static bool TryParse (string s, out byte result) {
- try {
- return Parse (s, true, out result);
- }
- catch (Exception) {
- result = 0;
- return false;
- }
+ public static bool TryParse (string s, out byte result)
+ {
+ return TryParse (s, NumberStyles.Integer, null, out result);
}
- public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out byte result) {
- try {
- uint tmpResult;
-
- result = 0;
- if (!UInt32.TryParse (s, style, provider, out tmpResult))
- return false;
- if (tmpResult > Byte.MaxValue || tmpResult < Byte.MinValue)
- return false;
- result = (byte)tmpResult;
- return true;
- }
- catch (Exception) {
- result = 0;
+ public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out byte result)
+ {
+ uint tmpResult;
+ result = 0;
+
+ if (!UInt32.TryParse (s, style, provider, out tmpResult))
return false;
- }
+
+ if (tmpResult > Byte.MaxValue)
+ return false;
+
+ result = (byte)tmpResult;
+ return true;
}
-#endif
public override string ToString ()
{
- return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value));
+ return NumberFormatter.NumberToString (m_value, null);
}
public string ToString (string format)
public string ToString (IFormatProvider provider)
{
- return NumberFormatter.FormatGeneral (new NumberFormatter.NumberStore (m_value), provider);
+ return NumberFormatter.NumberToString (m_value, provider);
}
public string ToString (string format, IFormatProvider provider)
{
- NumberFormatInfo nfi = NumberFormatInfo.GetInstance (provider);
- return NumberFormatter.NumberToString (format, m_value, nfi);
+ return NumberFormatter.NumberToString (format, m_value, provider);
}
// =========== IConvertible Methods =========== //
return TypeCode.Byte;
}
- object IConvertible.ToType (Type conversionType, IFormatProvider provider)
+ object IConvertible.ToType (Type targetType, IFormatProvider provider)
{
- return System.Convert.ToType (m_value, conversionType, provider);
+ if (targetType == null)
+ throw new ArgumentNullException ("targetType");
+ return System.Convert.ToType (m_value, targetType, provider, false);
}
bool IConvertible.ToBoolean (IFormatProvider provider)
return System.Convert.ToSingle (m_value);
}
-/*
- string IConvertible.ToString (IFormatProvider provider)
- {
- return ToString("G", provider);
- }
-*/
-
ushort IConvertible.ToUInt16 (IFormatProvider provider)
{
return System.Convert.ToUInt16 (m_value);