2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System / UInt16.cs
blob8ca6a6eab38031b54144b8060bab10182058b8e0
1 //
2 // System.UInt16.cs
3 //
4 // Author:
5 // Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004 Novell (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Globalization;
32 namespace System
34 [Serializable]
35 [CLSCompliant (false)]
36 [System.Runtime.InteropServices.ComVisible (true)]
37 public struct UInt16 : IFormattable, IConvertible, IComparable, IComparable<UInt16>, IEquatable <UInt16>
39 public const ushort MaxValue = 0xffff;
40 public const ushort MinValue = 0;
42 internal ushort m_value;
44 public int CompareTo (object value)
46 if (value == null)
47 return 1;
49 if(!(value is System.UInt16))
50 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt16."));
52 return this.m_value - ((ushort) value);
55 public override bool Equals (object obj)
57 if (!(obj is System.UInt16))
58 return false;
60 return ((ushort) obj) == m_value;
63 public override int GetHashCode ()
65 return m_value;
68 public int CompareTo (ushort value)
70 return m_value - value;
73 public bool Equals (ushort obj)
75 return obj == m_value;
78 [CLSCompliant (false)]
79 public static ushort Parse (string s, IFormatProvider provider)
81 return Parse (s, NumberStyles.Integer, provider);
84 [CLSCompliant (false)]
85 public static ushort Parse (string s, NumberStyles style)
87 return Parse (s, style, null);
90 [CLSCompliant (false)]
91 public static ushort Parse (string s, NumberStyles style, IFormatProvider provider)
93 uint tmpResult = UInt32.Parse (s, style, provider);
94 if (tmpResult > UInt16.MaxValue)
95 throw new OverflowException (Locale.GetText ("Value too large."));
97 return (ushort) tmpResult;
100 [CLSCompliant(false)]
101 public static ushort Parse (string s)
103 return Parse (s, NumberStyles.Number, null);
106 [CLSCompliant(false)]
107 public static bool TryParse (string s, out ushort result)
109 return TryParse (s, NumberStyles.Integer, null, out result);
112 [CLSCompliant(false)]
113 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out ushort result)
115 uint tmpResult;
116 result = 0;
118 if (!UInt32.TryParse (s, style, provider, out tmpResult))
119 return false;
121 if (tmpResult > UInt16.MaxValue)
122 return false;
124 result = (ushort)tmpResult;
125 return true;
128 public override string ToString ()
130 return NumberFormatter.NumberToString (m_value, null);
133 public string ToString (IFormatProvider provider)
135 return NumberFormatter.NumberToString (m_value, provider);
138 public string ToString (string format)
140 return ToString (format, null);
143 public string ToString (string format, IFormatProvider provider)
145 return NumberFormatter.NumberToString (format, m_value, provider);
148 // =========== IConvertible Methods =========== //
149 public TypeCode GetTypeCode ()
151 return TypeCode.UInt16;
154 bool IConvertible.ToBoolean (IFormatProvider provider)
156 return System.Convert.ToBoolean (m_value);
159 byte IConvertible.ToByte (IFormatProvider provider)
161 return System.Convert.ToByte (m_value);
164 char IConvertible.ToChar (IFormatProvider provider)
166 return System.Convert.ToChar (m_value);
169 DateTime IConvertible.ToDateTime (IFormatProvider provider)
171 return System.Convert.ToDateTime (m_value);
174 decimal IConvertible.ToDecimal (IFormatProvider provider)
176 return System.Convert.ToDecimal (m_value);
179 double IConvertible.ToDouble (IFormatProvider provider)
181 return System.Convert.ToDouble (m_value);
184 short IConvertible.ToInt16 (IFormatProvider provider)
186 return System.Convert.ToInt16 (m_value);
189 int IConvertible.ToInt32 (IFormatProvider provider)
191 return System.Convert.ToInt32 (m_value);
194 long IConvertible.ToInt64 (IFormatProvider provider)
196 return System.Convert.ToInt64 (m_value);
199 sbyte IConvertible.ToSByte (IFormatProvider provider)
201 return System.Convert.ToSByte (m_value);
204 float IConvertible.ToSingle (IFormatProvider provider)
206 return System.Convert.ToSingle (m_value);
209 object IConvertible.ToType (Type targetType, IFormatProvider provider)
211 if (targetType == null)
212 throw new ArgumentNullException ("targetType");
214 return System.Convert.ToType (m_value, targetType, provider, false);
217 ushort IConvertible.ToUInt16 (IFormatProvider provider)
219 return m_value;
221 uint IConvertible.ToUInt32 (IFormatProvider provider)
223 return System.Convert.ToUInt32 (m_value);
226 ulong IConvertible.ToUInt64 (IFormatProvider provider)
228 return System.Convert.ToUInt64 (m_value);