字段ModifyDate从未分配给它,并且将始终具有其默认值0 [英] Field modifyDate is never assigned to, and will always have its default value 0

查看:62
本文介绍了字段ModifyDate从未分配给它,并且将始终具有其默认值0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么我收到此警告吗?我已经测试了代码并且可以正常工作,我得到了我应该在那里获得的价值.这是错误的警告还是我缺少什么?

Can someone please explain to me why I'm getting this warning? I've tested the code and it works, I get the value that I should be getting in there. Is this a false warning or is there something I'm missing?

[DataMember(Name = "modifyDate")]        
private long modifyDate;

public DateTime lastModified 
{
    get { return DateTimeConverter.FromUnixTime(modifyDate); }
}

推荐答案

这是编译器警告,这意味着编译器在编译时找不到可分配值的任何代码.但是,仍然有可能在 runtime 处分配一个值.在您的情况下,这是通过 DataContractJsonSerializer 完成的.

This is a compiler warning, which means the compiler cannot find any code at compile time that would assign a value. However, it is still possible that a value is assigned at runtime. In your case, that's done by DataContractJsonSerializer.

如果知道的话,可以通过写来禁用警告

If you know that, you can disable the warning by writing

#pragma warning disable 0649

在受影响的代码行之前和

before the affected line of code and

#pragma warning restore 0649

在代码行之后.

通常,您还应该添加注释,说明您为何使用该编译指示,例如

Usually you should also add a comment why you're using that pragma instruction, e.g.

// This field is assigned to by JSON deserialization

这篇关于字段ModifyDate从未分配给它,并且将始终具有其默认值0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆