none
?. Check is object null - What are the options? RRS feed

  • Question

  • Hello,
    small question.
    SortedDictionary<int, string> expectedNumberOfVariables = null;
    
    // Do something create the dictionary, in case of not create is null 
    
    // Works not, why?
    if (expectedNumberOfVariables?.Count != 0)
    {
    	if (!expectedNumberOfVariables.ContainsKey(5))
    	{
    		expectedNumberOfVariables.Add(5, "MyTest");
    	}
    }
    
    // Works well maybe there is a way to fill the Dictionary not with Add?
    if (expectedNumberOfVariables != null && expectedNumberOfVariables.Count != 0)
    {
    	//Log.Debug(....
    }
    
    if (expectedNumberOfVariables?.Count != 0)
    ?. is check of null.
    
    if I read a xml file, I use this
     var qryQuantity = Convert.ToInt32(Root.Element("TASK")?.Element("QUANTITY")?.Attribute("mustbe").Value);
    	  
    tz

    Do you know a good free tool to make a good reflaction to optimize the code?

    Refactor

    With best regards Markus




    Thursday, July 5, 2018 5:00 PM

Answers

  • I'm confused as to what you're asking. Your title asks one thing but your final statement asks something else.

    ?. is the null conditional operator. It's sole purpose is to replace the (very) common code you see around reference types.

    if (value != null)
        value.DoSomething();

    Any time you'd write an if statement like this you should be using the null conditional. But you have to be careful when using it as an expression.

    //This is of type int
    expectedNumberOfVariables.Count
    
    //This is of type int? because if expectedNumberOfVariables is null the right side is never evaluated, hence null
    expectedNumberOfVariables?.Count
    
    
    //Equivalent of expectedNumberOfVariables != null && expectedNumberOfVariables.Count != 0
    
    (expectedNumberOfVariables?.Count ?? 0) != 0
    

    In your "it doesn't work" section you are making the assumption that if the value was null then it returns 0 but it doesn't, it returns int? set to null. null != 0 so the if statement is true. The correct check is the one I gave where it checks for null and returns 0 as the expression value which then compares to 0 correctly.

    VS 2017 and higher have quick actions that should automatically recommend this replacement whenever it applies. You can also download additional analyzers and refactors from the VS marketplace. Microsoft Code Analysis 2017 is a good one to have but there are many others. In almost all cases they can not only recommend a change but also fix it for you automatically. You can even build your own using the VS SDK.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, July 5, 2018 6:28 PM
    Moderator
  • The result of ‘expectedNumberOfVariables?.Count’ is not an integer (int), but a nullable integer (int?). Since expectedNumberOfVariables is null, the result is null too, therefore if passes.

    Try an alternative:

       if( ( expectedNumberOfVariables?.Count ?? 0 ) != 0 ) …

    or:

       if( expectedNumberOfVariables?.Count > 0 ) …

    or:

       if( expectedNumberOfVariables?.Any() == true ) …

    or:

       if( expectedNumberOfVariables != null )

       {

          expectedNumberOfVariables[5] = "MyTest";

       }

     


    Thursday, July 5, 2018 6:30 PM

All replies

  • I'm confused as to what you're asking. Your title asks one thing but your final statement asks something else.

    ?. is the null conditional operator. It's sole purpose is to replace the (very) common code you see around reference types.

    if (value != null)
        value.DoSomething();

    Any time you'd write an if statement like this you should be using the null conditional. But you have to be careful when using it as an expression.

    //This is of type int
    expectedNumberOfVariables.Count
    
    //This is of type int? because if expectedNumberOfVariables is null the right side is never evaluated, hence null
    expectedNumberOfVariables?.Count
    
    
    //Equivalent of expectedNumberOfVariables != null && expectedNumberOfVariables.Count != 0
    
    (expectedNumberOfVariables?.Count ?? 0) != 0
    

    In your "it doesn't work" section you are making the assumption that if the value was null then it returns 0 but it doesn't, it returns int? set to null. null != 0 so the if statement is true. The correct check is the one I gave where it checks for null and returns 0 as the expression value which then compares to 0 correctly.

    VS 2017 and higher have quick actions that should automatically recommend this replacement whenever it applies. You can also download additional analyzers and refactors from the VS marketplace. Microsoft Code Analysis 2017 is a good one to have but there are many others. In almost all cases they can not only recommend a change but also fix it for you automatically. You can even build your own using the VS SDK.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, July 5, 2018 6:28 PM
    Moderator
  • The result of ‘expectedNumberOfVariables?.Count’ is not an integer (int), but a nullable integer (int?). Since expectedNumberOfVariables is null, the result is null too, therefore if passes.

    Try an alternative:

       if( ( expectedNumberOfVariables?.Count ?? 0 ) != 0 ) …

    or:

       if( expectedNumberOfVariables?.Count > 0 ) …

    or:

       if( expectedNumberOfVariables?.Any() == true ) …

    or:

       if( expectedNumberOfVariables != null )

       {

          expectedNumberOfVariables[5] = "MyTest";

       }

     


    Thursday, July 5, 2018 6:30 PM