3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
11 ** Purpose: This class will encapsulate a short and provide an
12 ** Object representation of it.
15 ===========================================================*/
20 using System.Globalization;
22 /// using System.Numerics;
24 using System.Runtime.InteropServices;
25 using System.Diagnostics.Contracts;
27 [System.Runtime.InteropServices.ComVisible(true)]
29 [System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]
31 public struct Int16 : IComparable, IFormattable, IConvertible
32 , IComparable<Int16>, IEquatable<Int16>
33 /// , IArithmetic<Int16>
35 public struct Int16 : IComparable, IFormattable, IConvertible
38 internal short m_value;
40 public const short MaxValue = (short)0x7FFF;
41 public const short MinValue = unchecked((short)0x8000);
43 // Compares this object to another object, returning an integer that
44 // indicates the relationship.
45 // Returns a value less than zero if this object
46 // null is considered to be less than any instance.
47 // If object is not of type Int16, this method throws an ArgumentException.
49 public int CompareTo(Object value) {
55 return m_value - ((Int16)value).m_value;
58 throw new ArgumentException(Environment.GetResourceString("Arg_MustBeInt16"));
61 public int CompareTo(Int16 value) {
62 return m_value - value;
65 public override bool Equals(Object obj) {
66 if (!(obj is Int16)) {
69 return m_value == ((Int16)obj).m_value;
72 [System.Runtime.Versioning.NonVersionable]
73 public bool Equals(Int16 obj)
75 return m_value == obj;
78 // Returns a HashCode for the Int16
79 public override int GetHashCode() {
80 return ((int)((ushort)m_value) | (((int)m_value) << 16));
84 [System.Security.SecuritySafeCritical] // auto-generated
85 public override String ToString() {
86 Contract.Ensures(Contract.Result<String>() != null);
87 return Number.FormatInt32(m_value, null, NumberFormatInfo.CurrentInfo);
90 [System.Security.SecuritySafeCritical] // auto-generated
91 public String ToString(IFormatProvider provider) {
92 Contract.Ensures(Contract.Result<String>() != null);
93 return Number.FormatInt32(m_value, null, NumberFormatInfo.GetInstance(provider));
96 public String ToString(String format) {
97 Contract.Ensures(Contract.Result<String>() != null);
98 return ToString(format, NumberFormatInfo.CurrentInfo);
101 public String ToString(String format, IFormatProvider provider) {
102 Contract.Ensures(Contract.Result<String>() != null);
103 return ToString(format, NumberFormatInfo.GetInstance(provider));
106 [System.Security.SecuritySafeCritical] // auto-generated
107 private String ToString(String format, NumberFormatInfo info) {
108 Contract.Ensures(Contract.Result<String>() != null);
110 if (m_value<0 && format!=null && format.Length>0 && (format[0]=='X' || format[0]=='x')) {
111 uint temp = (uint)(m_value & 0x0000FFFF);
112 return Number.FormatUInt32(temp,format, info);
114 return Number.FormatInt32(m_value, format, info);
117 public static short Parse(String s) {
118 return Parse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
121 public static short Parse(String s, NumberStyles style) {
122 NumberFormatInfo.ValidateParseStyleInteger(style);
123 return Parse(s, style, NumberFormatInfo.CurrentInfo);
126 public static short Parse(String s, IFormatProvider provider) {
127 return Parse(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
130 public static short Parse(String s, NumberStyles style, IFormatProvider provider) {
131 NumberFormatInfo.ValidateParseStyleInteger(style);
132 return Parse(s, style, NumberFormatInfo.GetInstance(provider));
135 private static short Parse(String s, NumberStyles style, NumberFormatInfo info) {
139 i = Number.ParseInt32(s, style, info);
141 catch(OverflowException e) {
142 throw new OverflowException(Environment.GetResourceString("Overflow_Int16"), e);
145 // We need this check here since we don't allow signs to specified in hex numbers. So we fixup the result
146 // for negative numbers
147 if ((style & NumberStyles.AllowHexSpecifier) != 0) { // We are parsing a hexadecimal number
148 if ((i < 0) || (i > UInt16.MaxValue)) {
149 throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
154 if (i < MinValue || i > MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
158 public static bool TryParse(String s, out Int16 result) {
159 return TryParse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
162 public static bool TryParse(String s, NumberStyles style, IFormatProvider provider, out Int16 result) {
163 NumberFormatInfo.ValidateParseStyleInteger(style);
164 return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);
167 private static bool TryParse(String s, NumberStyles style, NumberFormatInfo info, out Int16 result) {
171 if (!Number.TryParseInt32(s, style, info, out i)) {
175 // We need this check here since we don't allow signs to specified in hex numbers. So we fixup the result
176 // for negative numbers
177 if ((style & NumberStyles.AllowHexSpecifier) != 0) { // We are parsing a hexadecimal number
178 if ((i < 0) || i > UInt16.MaxValue) {
185 if (i < MinValue || i > MaxValue) {
193 // IConvertible implementation
196 public TypeCode GetTypeCode() {
197 return TypeCode.Int16;
202 bool IConvertible.ToBoolean(IFormatProvider provider) {
203 return Convert.ToBoolean(m_value);
207 char IConvertible.ToChar(IFormatProvider provider) {
208 return Convert.ToChar(m_value);
212 sbyte IConvertible.ToSByte(IFormatProvider provider) {
213 return Convert.ToSByte(m_value);
217 byte IConvertible.ToByte(IFormatProvider provider) {
218 return Convert.ToByte(m_value);
222 short IConvertible.ToInt16(IFormatProvider provider) {
227 ushort IConvertible.ToUInt16(IFormatProvider provider) {
228 return Convert.ToUInt16(m_value);
232 int IConvertible.ToInt32(IFormatProvider provider) {
233 return Convert.ToInt32(m_value);
237 uint IConvertible.ToUInt32(IFormatProvider provider) {
238 return Convert.ToUInt32(m_value);
242 long IConvertible.ToInt64(IFormatProvider provider) {
243 return Convert.ToInt64(m_value);
247 ulong IConvertible.ToUInt64(IFormatProvider provider) {
248 return Convert.ToUInt64(m_value);
252 float IConvertible.ToSingle(IFormatProvider provider) {
253 return Convert.ToSingle(m_value);
257 double IConvertible.ToDouble(IFormatProvider provider) {
258 return Convert.ToDouble(m_value);
262 Decimal IConvertible.ToDecimal(IFormatProvider provider) {
263 return Convert.ToDecimal(m_value);
267 DateTime IConvertible.ToDateTime(IFormatProvider provider) {
268 throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Int16", "DateTime"));
272 Object IConvertible.ToType(Type type, IFormatProvider provider) {
273 return Convert.DefaultToType((IConvertible)this, type, provider);
278 /// // IArithmetic<Int16> implementation
281 /// /// <internalonly/>
282 /// Int16 IArithmetic<Int16>.AbsoluteValue(out bool overflowed) {
283 /// overflowed = (m_value == MinValue); // -m_value overflows
284 /// return (Int16) (m_value < 0 ? -m_value : m_value);
287 /// /// <internalonly/>
288 /// Int16 IArithmetic<Int16>.Negate(out bool overflowed) {
289 /// overflowed = (m_value == MinValue); // Negate(MinValue) overflows
290 /// return (Int16) (-m_value);
293 /// /// <internalonly/>
294 /// Int16 IArithmetic<Int16>.Sign(out bool overflowed) {
295 /// overflowed = false;
296 /// return (Int16) (m_value >= 0 ? (m_value == 0 ? 0 : 1) : -1);
299 /// /// <internalonly/>
300 /// Int16 IArithmetic<Int16>.Add(Int16 addend, out bool overflowed) {
301 /// int i = ((int)m_value) + addend;
302 /// overflowed = (i > MaxValue || i < MinValue);
303 /// return (Int16) i;
306 /// /// <internalonly/>
307 /// Int16 IArithmetic<Int16>.Subtract(Int16 subtrahend, out bool overflowed) {
308 /// int i = ((int)m_value)- subtrahend;
309 /// overflowed = (i > MaxValue || i < MinValue);
310 /// return (Int16) i;
313 /// /// <internalonly/>
314 /// Int16 IArithmetic<Int16>.Multiply(Int16 multiplier, out bool overflowed) {
315 /// long l = ((long)m_value) * multiplier;
316 /// overflowed = (l > MaxValue || l < MinValue);
317 /// return (Int16) l;
321 /// /// <internalonly/>
322 /// Int16 IArithmetic<Int16>.Divide(Int16 divisor, out bool overflowed) {
323 /// // signed integer division can overflow. Consider the following
324 /// // 8-bit case: -128/-1 = 128.
325 /// // 128 won't fit into a signed 8-bit integer, instead you will end up
328 /// // Because of this corner case, we must check if the numerator
329 /// // is MinValue and if the denominator is -1.
331 /// overflowed = (divisor == -1 && m_value == MinValue);
332 /// return (Int16) unchecked(m_value / divisor);
335 /// /// <internalonly/>
336 /// Int16 IArithmetic<Int16>.DivideRemainder(Int16 divisor, out Int16 remainder, out bool overflowed) {
337 /// remainder = (Int16) (m_value % divisor);
338 /// overflowed = (divisor == -1 && m_value == MinValue);
339 /// return (Int16) unchecked(m_value / divisor);
342 /// /// <internalonly/>
343 /// Int16 IArithmetic<Int16>.Remainder(Int16 divisor, out bool overflowed) {
344 /// overflowed = false;
345 /// return (Int16) (m_value % divisor);
348 /// /// <internalonly/>
349 /// ArithmeticDescriptor<Int16> IArithmetic<Int16>.GetDescriptor() {
350 /// if (s_descriptor == null) {
351 /// s_descriptor = new Int16ArithmeticDescriptor( ArithmeticCapabilities.One
352 /// | ArithmeticCapabilities.Zero
353 /// | ArithmeticCapabilities.MaxValue
354 /// | ArithmeticCapabilities.MinValue);
356 /// return s_descriptor;
359 /// private static Int16ArithmeticDescriptor s_descriptor;
361 /// class Int16ArithmeticDescriptor : ArithmeticDescriptor<Int16> {
362 /// public Int16ArithmeticDescriptor(ArithmeticCapabilities capabilities) : base(capabilities) {}
364 /// public override Int16 One {
366 /// return (Int16) 1;
370 /// public override Int16 Zero {
372 /// return (Int16) 0;
376 /// public override Int16 MinValue {
378 /// return Int16.MinValue;
382 /// public override Int16 MaxValue {
384 /// return Int16.MaxValue;
388 ///#endif // #if GENERICS_WORK