Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my addition of 2 shorts causing a casting compile error due to ints?

Tags:

c#

casting

In my code i have the following code:

Order = config.DeploymentSteps.Select(x => x.Order).DefaultIfEmpty().Max() + 1;

This gives me the error Cannot implicitly convert type 'int' to 'short'. As a Reference Order and x.Order are both shorts, and Max() is correctly returning a short (I have verified this). So I get it, it thinks the 1 is an integer and erroring. So I changed it to:

Order = config.DeploymentSteps.Select(x => x.Order).DefaultIfEmpty().Max() + (short)1;

I'm now still getting the same compile. So maybe it's not casting it right, so I tried changing it to

Order = config.DeploymentSteps.Select(x => x.Order).DefaultIfEmpty().Max() + Convert.ToInt16(1);

Yet I still get the same error. Finally I got it to work by converting the whole expression:

Order = Convert.ToInt16(config.DeploymentSteps.Select(x => x.Order).DefaultIfEmpty().Max() + 1);

Why can't I cast the 1 to a short and add it to another short, without casting the whole thing?

like image 978
KallDrexx Avatar asked Oct 16 '25 13:10

KallDrexx


1 Answers

It is because short + short = int.

Eric Lippert explains it here.

He says:

Why is short plus short result in int?

Well, suppose short plus short was short and see what happens:

short[] prices = { 10000, 15000, 11000 }; short average = (prices[0] + prices[1] + prices[2]) / 3; And the average is, of course, -9845 if this calculation is done in shorts. The sum is larger than the largest possible short, so it wraps around to negative, and then you divide the negative number.

In a world where integer arithmetic wraps around it is much more sensible to do all the calculations in int, a type which is likely to have enough range for typical calculations to not overflow.

like image 144
kevev22 Avatar answered Oct 19 '25 05:10

kevev22