简体   繁体   English

为什么.NET在错误的IF语句上执行代码?

[英]Why .NET executes code on false IF statement?

I have following code: 我有以下代码:

var dateFrom = DateTime.Parse(string.Format(string.Format("01.04.{0}", dateProperty.Value.AddYears(-1).Year))
if (object.nullablebool.HasValue ? object.nullablebool.Value : false  
&& (string == "V" || string == "N")
&& someDate.HasValue && object.SomeOtherDate.HasValue 
&& someDate.Value.Date > dateFrom.Date)
{
       >> Code
}

I have tested adding .Date or even specifiing exact year from the DateTime struct, but nothing worked. 我已经测试过从DateTime结构中添加.Date甚至指定确切的年份,但是没有任何效果。

When executing the code, even if 执行代码时,即使

someDate.Value.Date > dateFrom.Date

equals 1700 > 2018, the code executed as if it was true, even though the debugger says it´s false. 等于1700> 2018,即使调试器说它是错误的,代码也会像是正确的那样执行。

When I removed this part from the condition, following code: 当我从条件中删除此部分时,以下代码:

someDate.HasValue && object.SomeOtherDate.HasValue

When I made someDate null, so someDate.HasValue is false, the if statement still executes as true. 当我使someDate为null时,所以someDate.HasValue为false时,if语句仍按true执行。

What did it fix? 它修复了什么? Taking these two conditions to another if: 如果满足以下两个条件,则将这两个条件同时考虑在内:

var dateFrom = DateTime.Parse(string.Format(string.Format("01.04.{0}", dateProperty.Value.AddYears(-1).Year))
if (object.nullablebool.HasValue ? object.nullablebool.Value : false  
&& (string == "V" || string == "N"))    
{
     if (someDate.HasValue && object.SomeOtherDate.HasValue 
     && someDate.Value.Date > dateFrom.Date)
     {
          >> Code
     }
     else 
     {
          >> Code 
     }           
}

The code works, but it´s way too ugly. 该代码有效,但是太丑陋了。 I'm running on Visual Studio 2017 Pro. 我正在Visual Studio 2017 Pro上运行。 Any ideas why it behaves like that? 有什么想法为什么它会那样吗? Executing false statements? 执行虚假陈述? Thanks 谢谢

Your if statement performs different then expected, because it is parsed different as you wouls expect. 您的if语句执行的结果与预期的不同,因为您所期望的解析的结果不同。 object.nullablebool.HasValue ? object.nullablebool.Value : false && ... object.nullablebool.HasValue ? object.nullablebool.Value : false && ... is parsed as object.nullablebool.HasValue ? object.nullablebool.Value : (false && ...) object.nullablebool.HasValue ? object.nullablebool.Value : false && ... 解析object.nullablebool.HasValue ? object.nullablebool.Value : (false && ...) object.nullablebool.HasValue ? object.nullablebool.Value : (false && ...) . object.nullablebool.HasValue ? object.nullablebool.Value : (false && ...) So if object.nullablebool has a value, thats the result of the condition. 因此,如果object.nullablebool 具有值,那就是条件的结果。 To fix this you have to add parenthesis like this: 要解决此问题,您必须添加如下括号:

if ((object.nullablebool.HasValue ? object.nullablebool.Value : false )
&& (string == "V" || string == "N") 
&& someDate.HasValue && object.SomeOtherDate.HasValue 
&& someDate.Value.Date > dateFrom.Date)
{
     // if body
}

Let's brush up your code (please, get rid of names like string , object ; change them into meanful names): 让我们重新整理一下代码(请删除诸如stringobject类的名称;将它们更改为有意义的名称):

  // You don't want any formatting but a simple constructor
  var dateFrom = new DateTime(dateProperty.Value.Year - 1, 4, 1);    

  // object.nullablebool == true - if object.nullablebool has value and the value is true
  if (object.nullablebool == true && (string == "V" || string == "N")) {
    // if someDate.Value is null the result will be false 
    // All we have to do is to propagate the null: ?. in someDate?.Date
    if (someDate?.Date > dateFrom.Date && object.SomeOtherDate.HasValue) {
      // Code
    }
    else {  
      // Code
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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