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();
                }
            }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-08
  • 2021-09-25
  • 2021-10-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-13
  • 2022-12-23
  • 2021-12-30
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案