简体   繁体   中英

Field modifyDate is never assigned to, and will always have its default value 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); }
}

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

after the line of code.

Usually you should also add a comment why you're using that pragma instruction, eg

// This field is assigned to by JSON deserialization

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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