c# - Visual Studio 2015 表示 'cast is redundant' 。为什么?

标签 c# visual-studio-2015 uwp

我有一张宽 888px 高 592px 的图片,宽高比为 3:2。

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

double aspectRatio = decoder.PixelWidth/decoder.PixelHeight;

下面给出了预期的正确值 1.5,但 Visual Studio 说“Cast is redundant”,但这是为什么呢?

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

最佳答案

您只需将 一个 uint 转换为 double 即可强制执行浮点运算,因此:

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

或:

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

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

关于c# - Visual Studio 2015 表示 'cast is redundant' 。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34040683/

相关文章:

html - Visual Studio HTML 格式化程序 : Hitting enter removes all indents in the previous line

c# - 如何在样式中设置绑定(bind)?

c# - 如何发送正文中包含字符串数组的 post 请求

C# 如何每秒递增一个值,递增值是根据 amount.text 文件

azure - 无法在 VS 2015 中构建 Azure 数据工厂项目

MySQL UWP "Could not load file or assembly"

listview - Xamarin.Forms ListView 禁用 UWP 上的选择

java - C# 中的 Point2D.Double

c# - C# 中的组合生成器

c++ - 在 Visual Studio 2015 中检测要与 C++ 宏一起使用的 SIMD 指令集