1 Replies - 251 Views - Last Post: 27 January 2013 - 03:25 AM Rate Topic: -----

#1 albert018  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-January 13

「int16」 converted into 「int32」 implicitly

Posted 26 January 2013 - 08:18 PM

The following segment of code kind of confused me .

        private void button1_Click(object sender, EventArgs e)
        {
            Int16 nTemp = 1;
            NumberTest(nTemp + 1);
        }
        private void NumberTest(Int16 v_nValue)
        {
            MessageBox.Show(v_nValue.ToString());
        }



When I was trying to compile these code , the vS2010 throw an error about type int16 cannot convert into short.

But it seems like the same type.

So I took a test about it.


private void button1_Click(object sender, EventArgs e)
        {
            Int16 nTemp = 1;
            MessageBox.Show((nTemp + 1).GetType().ToString());
        }




The type what I got is 「int32」!

Could someone tell me what it is happening ??

Is This A Good Question/Topic? 0
  • +

Replies To: 「int16」 converted into 「int32」 implicitly

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: 「int16」 converted into 「int32」 implicitly

Posted 27 January 2013 - 03:25 AM

Basically, nTemp in this expression - nTemp + 1 - is promoted to type Int32, meaning the expression becomes (in terms of types) Int32 + Int32. This means the result of the expression is also of type Int32.

If the result of such an expression was Int16 instead, there would be a greater chance that the calculation would overflow.

This post has been edited by CodingSup3rnatur@l-360: 27 January 2013 - 03:39 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1