Add unit test for AggregateException.GetBaseException that works on .net but is broke...
[mono.git] / mcs / class / corlib / System / Byte.cs
1 //
2 // System.Byte.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.Globalization;
34
35 namespace System
36 {
37         [Serializable]
38         [System.Runtime.InteropServices.ComVisible (true)]
39         public struct Byte : IFormattable, IConvertible, IComparable, IComparable<Byte>, IEquatable <Byte>
40         {
41                 public const byte MinValue = 0;
42                 public const byte MaxValue = 255;
43
44                 internal byte m_value;
45
46                 public int CompareTo (object value)
47                 {
48                         if (value == null)
49                                 return 1;
50
51                         if (!(value is System.Byte))
52                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Byte."));
53
54                         byte xv = (byte) value;
55
56                         if (m_value == xv)
57                                 return 0;
58                         if (m_value > xv)
59                                 return 1;
60                         else
61                                 return -1;
62                 }
63
64                 public override bool Equals (object obj)
65                 {
66                         if (!(obj is System.Byte))
67                                 return false;
68
69                         return ((byte) obj) == m_value;
70                 }
71
72                 public override int GetHashCode ()
73                 {
74                         return m_value;
75                 }
76
77                 public int CompareTo (byte value)
78                 {
79                         if (m_value == value)
80                                 return 0;
81                         if (m_value > value)
82                                 return 1;
83                         else
84                                 return -1;
85                 }
86
87                 public bool Equals (byte obj)
88                 {
89                         return m_value == obj;
90                 }
91
92                 public static byte Parse (string s, IFormatProvider provider)
93                 {
94                         return Parse (s, NumberStyles.Integer, provider);
95                 }
96
97                 public static byte Parse (string s, NumberStyles style)
98                 {
99                         return Parse (s, style, null);
100                 }
101
102                 public static byte Parse (string s, NumberStyles style, IFormatProvider provider)
103                 {
104                         uint tmpResult = UInt32.Parse (s, style, provider);
105                         if (tmpResult > Byte.MaxValue)
106                                 throw new OverflowException (Locale.GetText ("Value too large."));
107
108                         return (byte) tmpResult;
109                 }
110
111                 public static byte Parse (string s) 
112                 {
113                         return Parse (s, NumberStyles.Integer, null);
114                 }
115
116                 public static bool TryParse (string s, out byte result) 
117                 {
118                         return TryParse (s, NumberStyles.Integer, null, out result);
119                 }
120
121                 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out byte result) 
122                 {
123                         uint tmpResult;
124                         result = 0;
125                         
126                         if (!UInt32.TryParse (s, style, provider, out tmpResult))
127                                 return false;
128                                 
129                         if (tmpResult > Byte.MaxValue)
130                                 return false;
131                                 
132                         result = (byte)tmpResult;
133                         return true;
134                 }
135
136                 public override string ToString ()
137                 {
138                         return NumberFormatter.NumberToString (m_value, null);
139                 }
140
141                 public string ToString (string format)
142                 {
143                         return ToString (format, null);
144                 }
145
146                 public string ToString (IFormatProvider provider)
147                 {
148                         return NumberFormatter.NumberToString (m_value, provider);
149                 }
150
151                 public string ToString (string format, IFormatProvider provider)
152                 {
153                         return NumberFormatter.NumberToString (format, m_value, provider);
154                 }
155
156                 // =========== IConvertible Methods =========== //
157                 public TypeCode GetTypeCode ()
158                 {
159                         return TypeCode.Byte;
160                 }
161
162                 object IConvertible.ToType (Type targetType, IFormatProvider provider)
163                 {
164                         if (targetType == null)
165                                 throw new ArgumentNullException ("targetType");
166                         return System.Convert.ToType (m_value, targetType, provider, false);
167                 }
168
169                 bool IConvertible.ToBoolean (IFormatProvider provider)
170                 {
171                         return System.Convert.ToBoolean (m_value);
172                 }
173
174                 byte IConvertible.ToByte (IFormatProvider provider)
175                 {
176                         return m_value;
177                 }
178
179                 char IConvertible.ToChar (IFormatProvider provider)
180                 {
181                         return System.Convert.ToChar (m_value);
182                 }
183
184                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
185                 {
186                         throw new InvalidCastException ();
187                 }
188
189                 decimal IConvertible.ToDecimal (IFormatProvider provider)
190                 {
191                         return System.Convert.ToDecimal (m_value);
192                 }
193
194                 double IConvertible.ToDouble (IFormatProvider provider)
195                 {
196                         return System.Convert.ToDouble (m_value);
197                 }
198
199                 short IConvertible.ToInt16 (IFormatProvider provider)
200                 {
201                         return System.Convert.ToInt16 (m_value);
202                 }
203
204                 int IConvertible.ToInt32 (IFormatProvider provider)
205                 {
206                         return System.Convert.ToInt32 (m_value);
207                 }
208
209                 long IConvertible.ToInt64 (IFormatProvider provider)
210                 {
211                         return System.Convert.ToInt64 (m_value);
212                 }
213
214                 sbyte IConvertible.ToSByte (IFormatProvider provider)
215                 {
216                         return System.Convert.ToSByte (m_value);
217                 }
218
219                 float IConvertible.ToSingle (IFormatProvider provider)
220                 {
221                         return System.Convert.ToSingle (m_value);
222                 }
223
224                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
225                 {
226                         return System.Convert.ToUInt16 (m_value);
227                 }
228
229                 uint IConvertible.ToUInt32 (IFormatProvider provider)
230                 {
231                         return System.Convert.ToUInt32 (m_value);
232                 }
233
234                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
235                 {
236                         return System.Convert.ToUInt64 (m_value);
237                 }
238         }
239 }