mscorlib(4.0.0.0) API with additions
UInt16.cs
4 using System.Security;
5 
6 namespace System
7 {
10  [CLSCompliant(false)]
11  [ComVisible(true)]
12  [__DynamicallyInvokable]
13  public struct UInt16 : IComparable, IFormattable, IConvertible, IComparable<ushort>, IEquatable<ushort>
14  {
15  private ushort m_value;
16 
18  [__DynamicallyInvokable]
19  public const ushort MaxValue = 65535;
20 
22  [__DynamicallyInvokable]
23  public const ushort MinValue = 0;
24 
31  public int CompareTo(object value)
32  {
33  if (value == null)
34  {
35  return 1;
36  }
37  if (value is ushort)
38  {
39  return this - (ushort)value;
40  }
41  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeUInt16"));
42  }
43 
47  [__DynamicallyInvokable]
48  public int CompareTo(ushort value)
49  {
50  return this - value;
51  }
52 
57  [__DynamicallyInvokable]
58  public override bool Equals(object obj)
59  {
60  if (!(obj is ushort))
61  {
62  return false;
63  }
64  return this == (ushort)obj;
65  }
66 
71  [NonVersionable]
72  [__DynamicallyInvokable]
73  public bool Equals(ushort obj)
74  {
75  return this == obj;
76  }
77 
80  [__DynamicallyInvokable]
81  public override int GetHashCode()
82  {
83  return this;
84  }
85 
88  [SecuritySafeCritical]
89  [__DynamicallyInvokable]
90  public override string ToString()
91  {
92  return Number.FormatUInt32(this, null, NumberFormatInfo.CurrentInfo);
93  }
94 
98  [SecuritySafeCritical]
99  [__DynamicallyInvokable]
100  public string ToString(IFormatProvider provider)
101  {
102  return Number.FormatUInt32(this, null, NumberFormatInfo.GetInstance(provider));
103  }
104 
109  [SecuritySafeCritical]
110  [__DynamicallyInvokable]
111  public string ToString(string format)
112  {
113  return Number.FormatUInt32(this, format, NumberFormatInfo.CurrentInfo);
114  }
115 
122  [SecuritySafeCritical]
123  [__DynamicallyInvokable]
124  public string ToString(string format, IFormatProvider provider)
125  {
126  return Number.FormatUInt32(this, format, NumberFormatInfo.GetInstance(provider));
127  }
128 
138  [CLSCompliant(false)]
139  [__DynamicallyInvokable]
140  public static ushort Parse(string s)
141  {
142  return Parse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
143  }
144 
159  [CLSCompliant(false)]
160  [__DynamicallyInvokable]
161  public static ushort Parse(string s, NumberStyles style)
162  {
163  NumberFormatInfo.ValidateParseStyleInteger(style);
164  return Parse(s, style, NumberFormatInfo.CurrentInfo);
165  }
166 
177  [CLSCompliant(false)]
178  [__DynamicallyInvokable]
179  public static ushort Parse(string s, IFormatProvider provider)
180  {
181  return Parse(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
182  }
183 
199  [CLSCompliant(false)]
200  [__DynamicallyInvokable]
201  public static ushort Parse(string s, NumberStyles style, IFormatProvider provider)
202  {
203  NumberFormatInfo.ValidateParseStyleInteger(style);
204  return Parse(s, style, NumberFormatInfo.GetInstance(provider));
205  }
206 
207  private static ushort Parse(string s, NumberStyles style, NumberFormatInfo info)
208  {
209  uint num = 0u;
210  try
211  {
212  num = Number.ParseUInt32(s, style, info);
213  }
214  catch (OverflowException innerException)
215  {
216  throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"), innerException);
217  }
218  if (num > 65535)
219  {
220  throw new OverflowException(Environment.GetResourceString("Overflow_UInt16"));
221  }
222  return (ushort)num;
223  }
224 
230  [CLSCompliant(false)]
231  [__DynamicallyInvokable]
232  public static bool TryParse(string s, out ushort result)
233  {
234  return TryParse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
235  }
236 
247  [CLSCompliant(false)]
248  [__DynamicallyInvokable]
249  public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out ushort result)
250  {
251  NumberFormatInfo.ValidateParseStyleInteger(style);
252  return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);
253  }
254 
255  private static bool TryParse(string s, NumberStyles style, NumberFormatInfo info, out ushort result)
256  {
257  result = 0;
258  if (!Number.TryParseUInt32(s, style, info, out uint result2))
259  {
260  return false;
261  }
262  if (result2 > 65535)
263  {
264  return false;
265  }
266  result = (ushort)result2;
267  return true;
268  }
269 
273  {
274  return TypeCode.UInt16;
275  }
276 
281  [__DynamicallyInvokable]
283  {
284  return Convert.ToBoolean(this);
285  }
286 
290  [__DynamicallyInvokable]
292  {
293  return Convert.ToChar(this);
294  }
295 
299  [__DynamicallyInvokable]
301  {
302  return Convert.ToSByte(this);
303  }
304 
308  [__DynamicallyInvokable]
310  {
311  return Convert.ToByte(this);
312  }
313 
317  [__DynamicallyInvokable]
319  {
320  return Convert.ToInt16(this);
321  }
322 
326  [__DynamicallyInvokable]
328  {
329  return this;
330  }
331 
335  [__DynamicallyInvokable]
337  {
338  return Convert.ToInt32(this);
339  }
340 
344  [__DynamicallyInvokable]
346  {
347  return Convert.ToUInt32(this);
348  }
349 
353  [__DynamicallyInvokable]
355  {
356  return Convert.ToInt64(this);
357  }
358 
362  [__DynamicallyInvokable]
364  {
365  return Convert.ToUInt64(this);
366  }
367 
371  [__DynamicallyInvokable]
373  {
374  return Convert.ToSingle(this);
375  }
376 
380  [__DynamicallyInvokable]
382  {
383  return Convert.ToDouble(this);
384  }
385 
389  [__DynamicallyInvokable]
391  {
392  return Convert.ToDecimal(this);
393  }
394 
399  [__DynamicallyInvokable]
401  {
402  throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "UInt16", "DateTime"));
403  }
404 
409  [__DynamicallyInvokable]
410  object IConvertible.ToType(Type type, IFormatProvider provider)
411  {
412  return Convert.DefaultToType(this, type, provider);
413  }
414  }
415 }
float ToSingle(IFormatProvider provider)
Converts the value of this instance to an equivalent single-precision floating-point number using the...
Converts a base data type to another base data type.
Definition: Convert.cs:10
static long ToInt64(object value)
Converts the value of the specified object to a 64-bit signed integer.
Definition: Convert.cs:2506
static double ToDouble(object value)
Converts the value of the specified object to a double-precision floating-point number.
Definition: Convert.cs:3189
double ToDouble(IFormatProvider provider)
Converts the value of this instance to an equivalent double-precision floating-point number using the...
static bool TryParse(string s, out ushort result)
Tries to convert the string representation of a number to its 16-bit unsigned integer equivalent....
Definition: UInt16.cs:232
bool ToBoolean(IFormatProvider provider)
Converts the value of this instance to an equivalent Boolean value using the specified culture-specif...
TypeCode
Specifies the type of an object.
Definition: TypeCode.cs:9
static float ToSingle(object value)
Converts the value of the specified object to a single-precision floating-point number.
Definition: Convert.cs:2982
Definition: __Canon.cs:3
The exception that is thrown for invalid casting or explicit conversion.
override int GetHashCode()
Returns the hash code for this instance.
Definition: UInt16.cs:81
char ToChar(IFormatProvider provider)
Converts the value of this instance to an equivalent Unicode character using the specified culture-sp...
TypeCode GetTypeCode()
Returns the T:System.TypeCode for value type T:System.UInt16.
Definition: UInt16.cs:272
string ToString(IFormatProvider provider)
Converts the numeric value of this instance to its equivalent string representation using the specifi...
Definition: UInt16.cs:100
int CompareTo(ushort value)
Compares this instance to a specified 16-bit unsigned integer and returns an indication of their rela...
Definition: UInt16.cs:48
Provides a mechanism for retrieving an object to control formatting.
Represents an instant in time, typically expressed as a date and time of day. To browse the ....
Definition: DateTime.cs:13
static ushort Parse(string s, NumberStyles style, IFormatProvider provider)
Converts the string representation of a number in a specified style and culture-specific format to it...
Definition: UInt16.cs:201
Represents a 16-bit unsigned integer.
Definition: UInt16.cs:13
DateTime ToDateTime(IFormatProvider provider)
Converts the value of this instance to an equivalent T:System.DateTime using the specified culture-sp...
static NumberFormatInfo CurrentInfo
Gets a read-only T:System.Globalization.NumberFormatInfo that formats values based on the current cul...
sbyte ToSByte(IFormatProvider provider)
Converts the value of this instance to an equivalent 8-bit signed integer using the specified culture...
Defines a generalized type-specific comparison method that a value type or class implements to order ...
Definition: IComparable.cs:8
override bool Equals(object obj)
Returns a value indicating whether this instance is equal to a specified object.
Definition: UInt16.cs:58
NumberStyles
Determines the styles permitted in numeric string arguments that are passed to the Parse and TryParse...
Definition: NumberStyles.cs:10
static char ToChar(object value)
Converts the value of the specified object to a Unicode character.
Definition: Convert.cs:670
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static bool ToBoolean(object value)
Converts the value of a specified object to an equivalent Boolean value.
Definition: Convert.cs:457
decimal ToDecimal(IFormatProvider provider)
Converts the value of this instance to an equivalent T:System.Decimal number using the specified cult...
static ushort Parse(string s, IFormatProvider provider)
Converts the string representation of a number in a specified culture-specific format to its 16-bit u...
Definition: UInt16.cs:179
int ToInt32(IFormatProvider provider)
Converts the value of this instance to an equivalent 32-bit signed integer using the specified cultur...
static NumberFormatInfo GetInstance(IFormatProvider formatProvider)
Gets the T:System.Globalization.NumberFormatInfo associated with the specified T:System....
const ushort MinValue
Represents the smallest possible value of T:System.UInt16. This field is constant.
Definition: UInt16.cs:23
object ToType(Type conversionType, IFormatProvider provider)
Converts the value of this instance to an T:System.Object of the specified T:System....
static sbyte ToSByte(object value)
Converts the value of the specified object to an 8-bit signed integer.
Definition: Convert.cs:909
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
ushort ToUInt16(IFormatProvider provider)
Converts the value of this instance to an equivalent 16-bit unsigned integer using the specified cult...
static uint ToUInt32(object value)
Converts the value of the specified object to a 32-bit unsigned integer.
Definition: Convert.cs:2235
short ToInt16(IFormatProvider provider)
Converts the value of this instance to an equivalent 16-bit signed integer using the specified cultur...
static decimal ToDecimal(object value)
Converts the value of the specified object to an equivalent decimal number.
Definition: Convert.cs:3394
bool Equals(ushort obj)
Returns a value indicating whether this instance is equal to a specified T:System....
Definition: UInt16.cs:73
long ToInt64(IFormatProvider provider)
Converts the value of this instance to an equivalent 64-bit signed integer using the specified cultur...
The exception that is thrown when one of the arguments provided to a method is not valid.
const ushort MaxValue
Represents the largest possible value of T:System.UInt16. This field is constant.
Definition: UInt16.cs:19
static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out ushort result)
Tries to convert the string representation of a number in a specified style and culture-specific form...
Definition: UInt16.cs:249
static byte ToByte(object value)
Converts the value of the specified object to an 8-bit unsigned integer.
Definition: Convert.cs:1186
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition: IEquatable.cs:6
static ushort Parse(string s, NumberStyles style)
Converts the string representation of a number in a specified style to its 16-bit unsigned integer eq...
Definition: UInt16.cs:161
static short ToInt16(object value)
Converts the value of the specified object to a 16-bit signed integer.
Definition: Convert.cs:1452
Specifies that the class can be serialized.
override string ToString()
Converts the numeric value of this instance to its equivalent string representation.
Definition: UInt16.cs:90
ulong ToUInt64(IFormatProvider provider)
Converts the value of this instance to an equivalent 64-bit unsigned integer using the specified cult...
static ulong ToUInt64(object value)
Converts the value of the specified object to a 64-bit unsigned integer.
Definition: Convert.cs:2727
static int ToInt32(object value)
Converts the value of the specified object to a 32-bit signed integer.
Definition: Convert.cs:1974
byte ToByte(IFormatProvider provider)
Converts the value of this instance to an equivalent 8-bit unsigned integer using the specified cultu...
Defines methods that convert the value of the implementing reference or value type to a common langua...
Definition: IConvertible.cs:9
string ToString(string format)
Converts the numeric value of this instance to its equivalent string representation using the specifi...
Definition: UInt16.cs:111
string ToString(string format, IFormatProvider provider)
Converts the numeric value of this instance to its equivalent string representation using the specifi...
Definition: UInt16.cs:124
Provides functionality to format the value of an object into a string representation.
Definition: IFormattable.cs:8
uint ToUInt32(IFormatProvider provider)
Converts the value of this instance to an equivalent 32-bit unsigned integer using the specified cult...
static ushort Parse(string s)
Converts the string representation of a number to its 16-bit unsigned integer equivalent.
Definition: UInt16.cs:140
int CompareTo(object value)
Compares this instance to a specified object and returns an indication of their relative values.
Definition: UInt16.cs:31
Provides culture-specific information for formatting and parsing numeric values.