Alex Rivera | Logout

Why is the result of a subtraction of an Int16 parameter from an Int16 variable an Int32?

Asked 2009-07-05T19:27:14.483
10

Possible Duplicate:
byte + byte = int… why?

I have a method like this:

 void Method(short parameter)
 {
     short localVariable = 0;
     var result = localVariable - parameter;
 }

Why is the result an Int32 instead of an Int16?

Edit
Report

1 Answer

1

I think its done automatically done to avoid overflow,

lets say you do something like this.

Short result = Short.MaxValue + Short.MaxValue; 

The result clearly wouldn't fit in a short.

one thing i don't understand is then why not do it for int32 too which would automatically convert to long???

answered 2009-07-05T19:40:11.947

Your Answer