简体   繁体   English

为什么根据ReSharper将double-to-int强制转换分配给动态变量是多余的?

[英]Why is double-to-int cast being assigned to a dynamic variable redundant according to ReSharper?

I have an assignment to a dynamic viewbag variable, which has to be an integer (yes, integer, without a fractional part, which breaks the libraries I'm using): 我为动态viewbag变量分配了一个变量,该变量必须是整数(是,整数, 没有小数部分,这会破坏我正在使用的库):

ViewBag.Autosubmit = (int)(SomeDate.Subtract(new DateTime(1970, 1, 1)).TotalSeconds);

TimeSpan.TotalSeconds returns a double , and to use it, I'm casting it into an int . TimeSpan.TotalSeconds返回一个double ,为了使用它,我将其转换为int But ReSharper says that casting to int is redundant. 但是ReSharper表示强制转换为int是多余的。 If I don't cast, the number stays as a double and my page doesn't work correctly. 如果我不进行转换,则该数字将保持为两倍,并且我的页面无法正常工作。

Why is ReSharper telling me the cast is redundant? 为什么ReSharper告诉我演员表是多余的?

I think this is because the cast is redundant as far as c# is concerned.. you are casting it but then putting it into a dynamic, which doesnt care if its an int or a double.. 我认为这是因为就C#而言,强制转换是多余的..您正在强制转换,但随后将其放入动态变量中,它不在乎它是int还是double。

If you explicitly cast it to an int.. like Lasse V. Karlsen mentioned 如果您将其明确转换为整数,例如Lasse V. Karlsen提到的

int autoSubmit = (int)(SomeDate.Subtract(new DateTime(1970, 1, 1)).TotalSeconds);
ViewBag.Autosubmit = autoSubmit;

I believe the message will then go away. 我相信该信息将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM