won*_*nea 4 c# resharper nullable
我遇到了以下代码的一个奇怪的问题.尽管Resharper强调了代码段(autorefresh == null),但它编译得很好,通知我Expression总是错误的
bool? autorefresh = Properties.Settings.Default.autorefresh;
autorefresh = (autorefresh == null) ? false : autorefresh;
Enabled = (bool)autorefresh;
Run Code Online (Sandbox Code Playgroud)
任何想法如何更好地解决这个问题?
编辑07/02/2012 16:52
Properties.Settings.Default.autorefresh
Run Code Online (Sandbox Code Playgroud)
以上是a bool,而不是a string.
我想你想要的是:
Enabled = Properties.Settings.Default.autorefresh ?? false;
Run Code Online (Sandbox Code Playgroud)
根据您的评论,您似乎不必将值分配autorefresh给a Nullable<bool>.在保护数据方面,Settings将返回默认值,为该类型,如果它是无效的或丢失(这将是false对boolean的).因此,您的代码应该只是:
Enabled = Properties.Settings.Default.autorefresh;
Run Code Online (Sandbox Code Playgroud)