首页 > 其他 > 详细

cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)

时间:2020-05-08 14:34:52      阅读:81      评论:0      收藏:0      [点我收藏+]

cannot implicitly convert type ‘bool?‘ to ‘bool‘. An explicit conversion exists (are you missing a cast?)

As the others stated bool? is not equal to bool. bool? can also be null, see Nullable<t> (msdn).

If you know what the null state wants to imply, you easily can use the ?? - null-coalescing operator (msdn) to convert your bool? to bool without any side effects (Exception).

Example:

//Let´s say "chkDisplay.IsChecked = null" has the same meaning as "chkDisplay.IsChecked = false" for you
//Let "check" be the value of "chkDisplay.IsChecked", unless "chkDisplay.IsChecked" is null, in which case "check = false"

bool check = chkDisplay.IsChecked ?? false;

 

举例

   var systemWeb = Element.Element("system.web");
            var compilation = systemWeb?.Element("compilation");
            var assemblies = compilation?.Element("assemblies");
            if (assemblies != null)
            {
                foreach (var li in RemoveList)
                {
                    IEnumerable<XElement> targetElements = assemblies.Elements("add").Where(s => s.Attribute("assembly").Value.Contains(li));
                    targetElements.Remove();
                }
            }

需要修改为

 var systemWeb = Element.Element("system.web");
            var compilation = systemWeb?.Element("compilation");
            var assemblies = compilation?.Element("assemblies");
            if (assemblies != null)
            {
                foreach (var li in RemoveList)
                {
                    IEnumerable<XElement> targetElements = assemblies.Elements("add")
                        .Where(s => s.Attribute("assembly")?.Value.Contains(li) ?? false);
                    targetElements.Remove();
                }
            }

 

cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)

原文:https://www.cnblogs.com/chucklu/p/12849486.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!