52

I wrote the following small program to print out the Fibonacci sequence:

static void Main(string[] args)
{
    Console.Write("Please give a value for n:");
    Int16 n = Int16.Parse(Console.ReadLine());

    Int16 firstNo = 0;
    Int16 secondNo = 1;

    Console.WriteLine(firstNo);
    Console.WriteLine(secondNo);

    for (Int16 i = 0; i < n; i++)
    {
        //Problem on this line                    
        Int16 answer = firstNo + secondNo;

        Console.WriteLine(answer);

        firstNo = secondNo;
        secondNo = answer;
    }

    Console.ReadLine();

}

The compilation message is:

Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?)

Since everything involved is an Int16 (short) then why are there any implicit conversions going on? And more specificially why the failure here (and not when initially assigning an int to the variable)?

An explanation would be much appreciated.

5
  • What line is error on? And why are you using shorts out of interest?
    – Tom Gullen
    May 4, 2011 at 11:15
  • 4
    @Tom id assume the line where he has the comment Problem on this line
    – Manatherin
    May 4, 2011 at 11:16
  • Related:stackoverflow.com/questions/1084668/… May 4, 2011 at 11:19
  • 1
    I suggest you simply use int instead, unless there is a very good reason to use Int16
    – Jon B
    May 4, 2011 at 11:21
  • I was simply experimenting by using an Int16 so I could see the sequence start again (when the width of Int16 can no longer hold the too large value) May 4, 2011 at 11:24

9 Answers 9

97

Microsoft converts your Int16 variables into Int32 when doing the add function.

Change the following:

Int16 answer = firstNo + secondNo;

into...

Int16 answer = (Int16)(firstNo + secondNo);
2
  • Confusing, why is it designed like that? Does this cause problems when adding two Int64's together (and they won't fit in the space of Int32)? May 4, 2011 at 11:22
  • Multiple reasons, some other answers point to posts explaining the why.
    – myermian
    May 4, 2011 at 11:40
8

Read Eric Lippert 's answers to these questions

6

Adding two Int16 values result in an Int32 value. You will have to cast it to Int16:

Int16 answer = (Int16) (firstNo + secondNo);

You can avoid this problem by switching all your numbers to Int32.

0
3

The result of summing two Int16 variables is an Int32:

Int16 i1 = 1;
Int16 i2 = 2;
var result = i1 + i2;
Console.WriteLine(result.GetType().Name);

It outputs Int32.

0
3

The problem is, that adding two Int16 results in an Int32 as others have already pointed out.
Your second question, why this problem doesn't already occur at the declaration of those two variables is explained here: http://msdn.microsoft.com/en-us/library/ybs77ex4%28v=VS.71%29.aspx:

short x = 32767;

In the preceding declaration, the integer literal 32767 is implicitly converted from int to short. If the integer literal does not fit into a short storage location, a compilation error will occur.

So, the reason why it works in your declaration is simply that the literals provided are known to fit into a short.

2

The plus operator converts operands to int first and then does the addition. So the result is the int. You need to cast it back to short explicitly because conversions from a "longer" type to "shorter" type a made explicit, so that you don't loose data accidentally with an implicit cast.

As to why int16 is cast to int, the answer is, because this is what is defined in C# spec. And C# is this way is because it was designed to closely match to the way how CLR works, and CLR has only 32/64 bit arithmetic and not 16 bit. Other languages on top of CLR may choose to expose this differently.

2

For some strange reason, you can use the += operator to add shorts.

short answer = 0;
short firstNo = 1;
short secondNo = 2;

answer += firstNo;
answer += secondNo;
1

The line

 Int16 answer = firstNo + secondNo;

is interpreted as

 Int16 answer = (Int32) (firstNo + secondNo);

Simply because there is no such thing as Int16 arithmetic.

The simple solution: Do not use Int16. Use Int32 or simply int.

int is your default integer type. short and long are only used in special cases.

0

That's because the result of adding two Int16 is an Int32. Check the "conversions" paragraph here: http://msdn.microsoft.com/en-us/library/ybs77ex4%28v=vs.71%29.aspx

Not the answer you're looking for? Browse other questions tagged or ask your own question.