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

use*_*851 9 c# visual-studio-2015 uwp

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

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

double aspectRatio = decoder.PixelWidth / decoder.PixelHeight;

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

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

Chr*_*isF 17

您只需要将其中一个 uint强制转换为强制浮点运算,因此:

double aspectRatio = decoder.PixelWidth / (double)decoder.PixelHeight;
Run Code Online (Sandbox Code Playgroud)

要么:

double aspectRatio = (double)decoder.PixelWidth / decoder.PixelHeight;
Run Code Online (Sandbox Code Playgroud)

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