mscorlib(4.0.0.0) API with additions
Int16.cs
4 using System.Security;
5 
6 namespace System
7 {
10  [ComVisible(true)]
11  [__DynamicallyInvokable]
13  {
14  internal short m_value;
15 
17  [__DynamicallyInvokable]
18  public const short MaxValue = 32767;
19 
21  [__DynamicallyInvokable]
22  public const short MinValue = -32768;
23 
30  public int CompareTo(object value)
31  {
32  if (value == null)
33  {
34  return 1;
35  }
36  if (value is short)
37  {
38  return this - (short)value;
39  }
40  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeInt16"));
41  }
42 
46  [__DynamicallyInvokable]
47  public int CompareTo(short value)
48  {
49  return this - value;
50  }
51 
56  [__DynamicallyInvokable]
57  public override bool Equals(object obj)
58  {
59  if (!(obj is short))
60  {
61  return false;
62  }
63  return this == (short)obj;
64  }
65 
70  [NonVersionable]
71  [__DynamicallyInvokable]
72  public bool Equals(short obj)
73  {
74  return this == obj;
75  }
76 
79  [__DynamicallyInvokable]
80  public override int GetHashCode()
81  {
82  return (ushort)this | (this << 16);
83  }
84 
87  [SecuritySafeCritical]
88  [__DynamicallyInvokable]
89  public override string ToString()
90  {
91  return Number.FormatInt32(this, null, NumberFormatInfo.CurrentInfo);
92  }
93 
97  [SecuritySafeCritical]
98  [__DynamicallyInvokable]
99  public string ToString(IFormatProvider provider)
100  {
101  return Number.FormatInt32(this, null, NumberFormatInfo.GetInstance(provider));
102  }
103 
107  [__DynamicallyInvokable]
108  public string ToString(string format)
109  {
110  return ToString(format, NumberFormatInfo.CurrentInfo);
111  }
112 
117  [__DynamicallyInvokable]
118  public string ToString(string format, IFormatProvider provider)
119  {
120  return ToString(format, NumberFormatInfo.GetInstance(provider));
121  }
122 
123  [SecuritySafeCritical]
124  private string ToString(string format, NumberFormatInfo info)
125  {
126  if (this < 0 && format != null && format.Length > 0 && (format[0] == 'X' || format[0] == 'x'))
127  {
128  uint value = (uint)(this & 0xFFFF);
129  return Number.FormatUInt32(value, format, info);
130  }
131  return Number.FormatInt32(this, format, info);
132  }
133 
143  [__DynamicallyInvokable]
144  public static short Parse(string s)
145  {
146  return Parse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
147  }
148 
163  [__DynamicallyInvokable]
164  public static short Parse(string s, NumberStyles style)
165  {
166  NumberFormatInfo.ValidateParseStyleInteger(style);
167  return Parse(s, style, NumberFormatInfo.CurrentInfo);
168  }
169 
180  [__DynamicallyInvokable]
181  public static short Parse(string s, IFormatProvider provider)
182  {
183  return Parse(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
184  }
185 
201  [__DynamicallyInvokable]
202  public static short Parse(string s, NumberStyles style, IFormatProvider provider)
203  {
204  NumberFormatInfo.ValidateParseStyleInteger(style);
205  return Parse(s, style, NumberFormatInfo.GetInstance(provider));
206  }
207 
208  private static short Parse(string s, NumberStyles style, NumberFormatInfo info)
209  {
210  int num = 0;
211  try
212  {
213  num = Number.ParseInt32(s, style, info);
214  }
215  catch (OverflowException innerException)
216  {
217  throw new OverflowException(Environment.GetResourceString("Overflow_Int16"), innerException);
218  }
219  if ((style & NumberStyles.AllowHexSpecifier) != 0)
220  {
221  if (num < 0 || num > 65535)
222  {
223  throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
224  }
225  return (short)num;
226  }
227  if (num < -32768 || num > 32767)
228  {
229  throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
230  }
231  return (short)num;
232  }
233 
239  [__DynamicallyInvokable]
240  public static bool TryParse(string s, out short result)
241  {
242  return TryParse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
243  }
244 
255  [__DynamicallyInvokable]
256  public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out short result)
257  {
258  NumberFormatInfo.ValidateParseStyleInteger(style);
259  return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);
260  }
261 
262  private static bool TryParse(string s, NumberStyles style, NumberFormatInfo info, out short result)
263  {
264  result = 0;
265  if (!Number.TryParseInt32(s, style, info, out int result2))
266  {
267  return false;
268  }
269  if ((style & NumberStyles.AllowHexSpecifier) != 0)
270  {
271  if (result2 < 0 || result2 > 65535)
272  {
273  return false;
274  }
275  result = (short)result2;
276  return true;
277  }
278  if (result2 < -32768 || result2 > 32767)
279  {
280  return false;
281  }
282  result = (short)result2;
283  return true;
284  }
285 
289  {
290  return TypeCode.Int16;
291  }
292 
297  [__DynamicallyInvokable]
299  {
300  return Convert.ToBoolean(this);
301  }
302 
306  [__DynamicallyInvokable]
308  {
309  return Convert.ToChar(this);
310  }
311 
315  [__DynamicallyInvokable]
317  {
318  return Convert.ToSByte(this);
319  }
320 
324  [__DynamicallyInvokable]
326  {
327  return Convert.ToByte(this);
328  }
329 
333  [__DynamicallyInvokable]
335  {
336  return this;
337  }
338 
342  [__DynamicallyInvokable]
344  {
345  return Convert.ToUInt16(this);
346  }
347 
351  [__DynamicallyInvokable]
353  {
354  return Convert.ToInt32(this);
355  }
356 
360  [__DynamicallyInvokable]
362  {
363  return Convert.ToUInt32(this);
364  }
365 
369  [__DynamicallyInvokable]
371  {
372  return Convert.ToInt64(this);
373  }
374 
378  [__DynamicallyInvokable]
380  {
381  return Convert.ToUInt64(this);
382  }
383 
387  [__DynamicallyInvokable]
389  {
390  return Convert.ToSingle(this);
391  }
392 
396  [__DynamicallyInvokable]
398  {
399  return Convert.ToDouble(this);
400  }
401 
405  [__DynamicallyInvokable]
407  {
408  return Convert.ToDecimal(this);
409  }
410 
415  [__DynamicallyInvokable]
417  {
418  throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Int16", "DateTime"));
419  }
420 
425  [__DynamicallyInvokable]
426  object IConvertible.ToType(Type type, IFormatProvider provider)
427  {
428  return Convert.DefaultToType(this, type, provider);
429  }
430  }
431 }
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
const short MinValue
Represents the smallest possible value of T:System.Int16. This field is constant.
Definition: Int16.cs:22
static double ToDouble(object value)
Converts the value of the specified object to a double-precision floating-point number.
Definition: Convert.cs:3189
static short 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: Int16.cs:202
override string ToString()
Converts the numeric value of this instance to its equivalent string representation.
Definition: Int16.cs:89
string ToString(string format, IFormatProvider provider)
Converts the numeric value of this instance to its equivalent string representation using the specifi...
Definition: Int16.cs:118
double ToDouble(IFormatProvider provider)
Converts the value of this instance to an equivalent double-precision floating-point number using the...
bool Equals(short obj)
Returns a value indicating whether this instance is equal to a specified T:System....
Definition: Int16.cs:72
const short MaxValue
Represents the largest possible value of an T:System.Int16. This field is constant.
Definition: Int16.cs:18
static bool TryParse(string s, out short result)
Converts the string representation of a number to its 16-bit signed integer equivalent....
Definition: Int16.cs:240
static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out short result)
Converts the string representation of a number in a specified style and culture-specific format to it...
Definition: Int16.cs:256
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
static short Parse(string s, NumberStyles style)
Converts the string representation of a number in a specified style to its 16-bit signed integer equi...
Definition: Int16.cs:164
override bool Equals(object obj)
Returns a value indicating whether this instance is equal to a specified object.
Definition: Int16.cs:57
Definition: __Canon.cs:3
The exception that is thrown for invalid casting or explicit conversion.
char ToChar(IFormatProvider provider)
Converts the value of this instance to an equivalent Unicode character using the specified culture-sp...
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
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
int CompareTo(short value)
Compares this instance to a specified 16-bit signed integer and returns an integer that indicates whe...
Definition: Int16.cs:47
NumberStyles
Determines the styles permitted in numeric string arguments that are passed to the Parse and TryParse...
Definition: NumberStyles.cs:10
override int GetHashCode()
Returns the hash code for this instance.
Definition: Int16.cs:80
static short Parse(string s)
Converts the string representation of a number to its 16-bit signed integer equivalent.
Definition: Int16.cs:144
string ToString(IFormatProvider provider)
Converts the numeric value of this instance to its equivalent string representation using the specifi...
Definition: Int16.cs:99
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 short Parse(string s, IFormatProvider provider)
Converts the string representation of a number in a specified culture-specific format to its 16-bit s...
Definition: Int16.cs:181
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....
static ushort ToUInt16(object value)
Converts the value of the specified object to a 16-bit unsigned integer.
Definition: Convert.cs:1707
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
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.
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
Specifies that the class can be serialized.
ulong ToUInt64(IFormatProvider provider)
Converts the value of this instance to an equivalent 64-bit unsigned integer using the specified cult...
int CompareTo(object value)
Compares this instance to a specified object and returns an integer that indicates whether the value ...
Definition: Int16.cs:30
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...
string ToString(string format)
Converts the numeric value of this instance to its equivalent string representation,...
Definition: Int16.cs:108
Defines methods that convert the value of the implementing reference or value type to a common langua...
Definition: IConvertible.cs:9
Represents a 16-bit signed integer.
Definition: Int16.cs:12
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...
TypeCode GetTypeCode()
Returns the T:System.TypeCode for value type T:System.Int16.
Definition: Int16.cs:288
Provides culture-specific information for formatting and parsing numeric values.