implicit conversion from short to int, long, float, double, or decimal. |
You cannot implicitly convert nonliteral numeric types of larger storage size to short (see Integral Types Table (C# Reference) for the storage sizes of integral types). Consider, for example, the following two short variables x and y: |
The following program will throw an error as shown in the picture, when you enter a value that is more than 32000 |
using System; //csc short_to_int.cs // short -32,768 to 32,767 Signed 16-bit integer //short to int, long, float, double, or decimal == implict //from int, long, float, double, or decimal to short== explicit namespace dtype { public class dtsb { public int conv( int int_lss, int int_more) { short sb1, sb2; sb1 = (short)int_lss; sb2 = (short)int_more; //short sh1 = (short)(int_less + ) ; Console.WriteLine("Number Less that 32000 : " + sb1 + " Number More than 32000: " + sb2); return sb1 ; } } class test { static void Main() { dtsb d = new dtsb(); Console.Write( "Enter a number lower or equal to 32000: "); string str = Console.ReadLine(); int low = short.Parse(str) ; Console.Write(" Number int_more than 32000 : "); str = Console.ReadLine(); int high = short.Parse( str); d.conv(low, high); } } } |