Puzzled about this simple line :(

maher33

New member
Joined
Feb 3, 2015
Messages
3
Programming Experience
Beginner
Can someone explain to me why this line is wrong:
Int16 pr1 = Convert.ToInt32(Console.ReadLine()) + Convert.ToInt32(Console.ReadLine());

but if Int16 is replaced with Int32 or Int64 or int, line is Error free.
 
You can't assign a 32bit number to a 16bit variable without explicitly converting it. It is a narrowing conversion that could fail or result in data loss, because not all possible 32bit values can fit in a 16bit storage.
 
You can't assign a 32bit number to a 16bit variable without explicitly converting it. It is a narrowing conversion that could fail or result in data loss, because not all possible 32bit values can fit in a 16bit storage.

thanks mate for reply. but 16 to 16 is also not possible; Int16 pr1 = Convert.ToInt16(Console.ReadLine()) + Convert.ToInt16(Console.ReadLine()); //also error
 
All arithmetic operators on 16bit values in C# widens to Int32. You need to convert or cast the result to 16bit if you need that.

You should also use the C# data types short/int/long in code, rather than the .Net framework structure types Int16/Int32/Int64.
 
All arithmetic operators on 16bit values in C# widens to Int32. You need to convert or cast the result to 16bit if you need that.

You should also use the C# data types short/int/long in code, rather than the .Net framework structure types Int16/Int32/Int64.

Thanks mate a lot. Sorry for late response.
 
Back
Top Bottom