Visual Studio 2015表示'演员是多余的'。为什么?

时间:2015-12-02 10:41:56

标签: c# visual-studio-2015 uwp

我的图像宽度为888像素,高度为592像素,宽高比:高度为3:2。

以下产生错误的值1,因为整数计算/截断,因为BitmapDecoder.PixelWidth和BitmapDecoder.PixelHeight都是uint(无符号整数),而decoder下面是BitmapDecoder对象。

double aspectRatio = decoder.PixelWidth / decoder.PixelHeight;

以下给出了1.5的预期正确值,但Visual Studio说“Cast是多余的”,但为什么呢?

double aspectRatio = (double)decoder.PixelWidth / (double)decoder.PixelHeight;

2 个答案:

答案 0 :(得分:14)

你只需要将一个的uints强制转换为加倍浮点运算,所以:

double aspectRatio = decoder.PixelWidth / (double)decoder.PixelHeight;

或:

double aspectRatio = (double)decoder.PixelWidth / decoder.PixelHeight;

就个人而言,我会选择后者,但这是一个意见问题。

答案 1 :(得分:2)

为了补充@ChrisF的答案,您可以在IL代码中很好地看到这一点,其中ng-click的单个转换将产生两个值的转换:

angular.element(someElement).parent()