2008-06-27 Andreas Nahr <ClassDevelopment@A-SoftTech.com>
[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 #if NET_2_0
39         [System.Runtime.InteropServices.ComVisible (true)]
40 #endif
41         public struct Byte : IFormattable, IConvertible, IComparable
42 #if NET_2_0
43                 , IComparable<Byte>, IEquatable <Byte>
44 #endif
45         {
46                 public const byte MinValue = 0;
47                 public const byte MaxValue = 255;
48
49                 internal byte m_value;
50
51                 public int CompareTo (object value)
52                 {
53                         if (value == null)
54                                 return 1;
55
56                         if (!(value is System.Byte))
57                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Byte."));
58
59                         byte xv = (byte) value;
60
61                         if (m_value == xv)
62                                 return 0;
63                         if (m_value > xv)
64                                 return 1;
65                         else
66                                 return -1;
67                 }
68
69                 public override bool Equals (object obj)
70                 {
71                         if (!(obj is System.Byte))
72                                 return false;
73
74                         return ((byte) obj) == m_value;
75                 }
76
77                 public override int GetHashCode ()
78                 {
79                         return m_value;
80                 }
81
82 #if NET_2_0
83                 public int CompareTo (byte value)
84                 {
85                         if (m_value == value)
86                                 return 0;
87                         if (m_value > value)
88                                 return 1;
89                         else
90                                 return -1;
91                 }
92
93                 public bool Equals (byte obj)
94                 {
95                         return m_value == obj;
96                 }
97 #endif
98
99                 public static byte Parse (string s, IFormatProvider provider)
100                 {
101                         return Parse (s, NumberStyles.Integer, provider);
102                 }
103
104                 public static byte Parse (string s, NumberStyles style)
105                 {
106                         return Parse (s, style, null);
107                 }
108
109                 public static byte Parse (string s, NumberStyles style, IFormatProvider provider)
110                 {
111                         uint tmpResult = UInt32.Parse (s, style, provider);
112                         if (tmpResult > Byte.MaxValue)
113                                 throw new OverflowException (Locale.GetText ("Value too large."));
114
115                         return (byte) tmpResult;
116                 }
117
118                 public static byte Parse (string s) 
119                 {
120                         return Parse (s, NumberStyles.Integer, null);
121                 }
122
123 #if NET_2_0
124                 public static bool TryParse (string s, out byte result) 
125                 {
126                         return TryParse (s, NumberStyles.Integer, null, out result);
127                 }
128
129                 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out byte result) 
130                 {
131                         uint tmpResult;
132                         result = 0;
133                         
134                         if (!UInt32.TryParse (s, style, provider, out tmpResult))
135                                 return false;
136                                 
137                         if (tmpResult > Byte.MaxValue)
138                                 return false;
139                                 
140                         result = (byte)tmpResult;
141                         return true;
142                 }
143 #endif
144
145                 public override string ToString ()
146                 {
147                         return NumberFormatter.NumberToString (m_value, null);
148                 }
149
150                 public string ToString (string format)
151                 {
152                         return ToString (format, null);
153                 }
154
155                 public string ToString (IFormatProvider provider)
156                 {
157                         return NumberFormatter.NumberToString (m_value, provider);
158                 }
159
160                 public string ToString (string format, IFormatProvider provider)
161                 {
162                         return NumberFormatter.NumberToString (format, m_value, provider);
163                 }
164
165                 // =========== IConvertible Methods =========== //
166                 public TypeCode GetTypeCode ()
167                 {
168                         return TypeCode.Byte;
169                 }
170
171                 object IConvertible.ToType (Type type, IFormatProvider provider)
172                 {
173                         return System.Convert.ToType (m_value, type, provider);
174                 }
175
176                 bool IConvertible.ToBoolean (IFormatProvider provider)
177                 {
178                         return System.Convert.ToBoolean (m_value);
179                 }
180
181                 byte IConvertible.ToByte (IFormatProvider provider)
182                 {
183                         return m_value;
184                 }
185
186                 char IConvertible.ToChar (IFormatProvider provider)
187                 {
188                         return System.Convert.ToChar (m_value);
189                 }
190
191                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
192                 {
193                         throw new InvalidCastException ();
194                 }
195
196                 decimal IConvertible.ToDecimal (IFormatProvider provider)
197                 {
198                         return System.Convert.ToDecimal (m_value);
199                 }
200
201                 double IConvertible.ToDouble (IFormatProvider provider)
202                 {
203                         return System.Convert.ToDouble (m_value);
204                 }
205
206                 short IConvertible.ToInt16 (IFormatProvider provider)
207                 {
208                         return System.Convert.ToInt16 (m_value);
209                 }
210
211                 int IConvertible.ToInt32 (IFormatProvider provider)
212                 {
213                         return System.Convert.ToInt32 (m_value);
214                 }
215
216                 long IConvertible.ToInt64 (IFormatProvider provider)
217                 {
218                         return System.Convert.ToInt64 (m_value);
219                 }
220
221                 sbyte IConvertible.ToSByte (IFormatProvider provider)
222                 {
223                         return System.Convert.ToSByte (m_value);
224                 }
225
226                 float IConvertible.ToSingle (IFormatProvider provider)
227                 {
228                         return System.Convert.ToSingle (m_value);
229                 }
230
231                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
232                 {
233                         return System.Convert.ToUInt16 (m_value);
234                 }
235
236                 uint IConvertible.ToUInt32 (IFormatProvider provider)
237                 {
238                         return System.Convert.ToUInt32 (m_value);
239                 }
240
241                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
242                 {
243                         return System.Convert.ToUInt64 (m_value);
244                 }
245         }
246 }