[System] handle arguments whose ToString returns null in String.Format
[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                         return CompareTo ((byte) value);
55                 }
56
57                 public override bool Equals (object obj)
58                 {
59                         if (!(obj is System.Byte))
60                                 return false;
61
62                         return ((byte) obj) == m_value;
63                 }
64
65                 public override int GetHashCode ()
66                 {
67                         return m_value;
68                 }
69
70                 public int CompareTo (byte value)
71                 {
72                         return m_value - value;
73                 }
74
75                 public bool Equals (byte obj)
76                 {
77                         return m_value == obj;
78                 }
79
80                 public static byte Parse (string s, IFormatProvider provider)
81                 {
82                         return Parse (s, NumberStyles.Integer, provider);
83                 }
84
85                 public static byte Parse (string s, NumberStyles style)
86                 {
87                         return Parse (s, style, null);
88                 }
89
90                 public static byte Parse (string s, NumberStyles style, IFormatProvider provider)
91                 {
92                         uint tmpResult = UInt32.Parse (s, style, provider);
93                         if (tmpResult > Byte.MaxValue)
94                                 throw new OverflowException (Locale.GetText ("Value too large."));
95
96                         return (byte) tmpResult;
97                 }
98
99                 public static byte Parse (string s) 
100                 {
101                         return Parse (s, NumberStyles.Integer, null);
102                 }
103
104                 public static bool TryParse (string s, out byte result) 
105                 {
106                         return TryParse (s, NumberStyles.Integer, null, out result);
107                 }
108
109                 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out byte result) 
110                 {
111                         uint tmpResult;
112                         result = 0;
113                         
114                         if (!UInt32.TryParse (s, style, provider, out tmpResult))
115                                 return false;
116                                 
117                         if (tmpResult > Byte.MaxValue)
118                                 return false;
119                                 
120                         result = (byte)tmpResult;
121                         return true;
122                 }
123
124                 public override string ToString ()
125                 {
126                         return NumberFormatter.NumberToString (m_value, null);
127                 }
128
129                 public string ToString (string format)
130                 {
131                         return ToString (format, null);
132                 }
133
134                 public string ToString (IFormatProvider provider)
135                 {
136                         return NumberFormatter.NumberToString (m_value, provider);
137                 }
138
139                 public string ToString (string format, IFormatProvider provider)
140                 {
141                         return NumberFormatter.NumberToString (format, m_value, provider);
142                 }
143
144                 // =========== IConvertible Methods =========== //
145                 public TypeCode GetTypeCode ()
146                 {
147                         return TypeCode.Byte;
148                 }
149
150                 object IConvertible.ToType (Type targetType, IFormatProvider provider)
151                 {
152                         if (targetType == null)
153                                 throw new ArgumentNullException ("targetType");
154                         return System.Convert.ToType (m_value, targetType, provider, false);
155                 }
156
157                 bool IConvertible.ToBoolean (IFormatProvider provider)
158                 {
159                         return System.Convert.ToBoolean (m_value);
160                 }
161
162                 byte IConvertible.ToByte (IFormatProvider provider)
163                 {
164                         return m_value;
165                 }
166
167                 char IConvertible.ToChar (IFormatProvider provider)
168                 {
169                         return System.Convert.ToChar (m_value);
170                 }
171
172                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
173                 {
174                         throw new InvalidCastException ();
175                 }
176
177                 decimal IConvertible.ToDecimal (IFormatProvider provider)
178                 {
179                         return System.Convert.ToDecimal (m_value);
180                 }
181
182                 double IConvertible.ToDouble (IFormatProvider provider)
183                 {
184                         return System.Convert.ToDouble (m_value);
185                 }
186
187                 short IConvertible.ToInt16 (IFormatProvider provider)
188                 {
189                         return System.Convert.ToInt16 (m_value);
190                 }
191
192                 int IConvertible.ToInt32 (IFormatProvider provider)
193                 {
194                         return System.Convert.ToInt32 (m_value);
195                 }
196
197                 long IConvertible.ToInt64 (IFormatProvider provider)
198                 {
199                         return System.Convert.ToInt64 (m_value);
200                 }
201
202                 sbyte IConvertible.ToSByte (IFormatProvider provider)
203                 {
204                         return System.Convert.ToSByte (m_value);
205                 }
206
207                 float IConvertible.ToSingle (IFormatProvider provider)
208                 {
209                         return System.Convert.ToSingle (m_value);
210                 }
211
212                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
213                 {
214                         return System.Convert.ToUInt16 (m_value);
215                 }
216
217                 uint IConvertible.ToUInt32 (IFormatProvider provider)
218                 {
219                         return System.Convert.ToUInt32 (m_value);
220                 }
221
222                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
223                 {
224                         return System.Convert.ToUInt64 (m_value);
225                 }
226         }
227 }