.Net:- Operator – The null-conditional operator ?.

Background

In this post we will cover the Null-Conditional Operator ( ?. ).

 

Series

Here are other posts that covers .Net Operators:-

  1. .Net:- Operator – The null-coalescing operator ??
    Date Published:- 2022-November-8th
    Link

 

Code

Code – HelloWorld

Code – HelloWorld

Outline

  1. Get String Length
  2. Print String and String Length

string FORMAT_WORLD_LENGTH = "{0}:- Length is {1}";
string strHelloWorld = "helloWorld";

Console.WriteLine
            (
                  FORMAT_WORLD_LENGTH
                , strHelloWorld
                , strHelloWorld.Length
            );

Output

Output – Text


helloWorld:- Length is 10

Code – HelloWorld

Code – Null

Code – Null

Outline

  1. Get String Length
  2. Print String and String Length
    • Fails as string is null

Code


string FORMAT_WORLD_LENGTH = "{0}:- Length is {1}";
string strNull = null;

Console.WriteLine
            (
                  FORMAT_WORLD_LENGTH
                , strNull
                , strNull.Length
            );

Output

Output – Text


Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at HelloWorld.Main () [0x00008] in <e877f77fc5404b3fa6220a9ba282a8d2>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
  at HelloWorld.Main () [0x00008] in <e877f77fc5404b3fa6220a9ba282a8d2>:0 

Output – Image

Code – Null – Operator !=

Outline

  1. Condition
    • Checks string to determine if it is null
    • The check is performed using the != operator
  2. If string is not null
    • Get String Length
    • Print String and String Length

Code


string FORMAT_WORLD_LENGTH = "{0}:- Length is {1}";
string strNull = null;

if (strNull != 0)
{

     Console.WriteLine
            (
                  FORMAT_WORLD_LENGTH
                , strNull
                , strNull.Length
            );

}

Code – Null – Null Conditional Method ( Operator ?. )

Outline

  1. Use ?.
    • strNull?.Length
      • The ?. means do not call the length method if the variable is null

Code


string FORMAT_WORLD_LENGTH = "{0}:- Length is {1}";
string strNull = null;

Console.WriteLine
            (
                  FORMAT_WORLD_LENGTH
                , strNull
                , strNull?.Length
            );

Output

Output – Text
:- Length is 
Output – Image

Code – Null – Null Conditional Method ( Operator ?. ) and null-coalescing operator ( ?? )

Outline

  1. Use ?.
    • strNull?.Length
      • The ?. means do not call the length method if the variable is null
  2. Use ??
    • ??
      • strNull?.Length ?? -1
        • If strNull is null, then display -1
      • strNull ?? ( “Variable ” + nameof(strNull) )
        • If strNull is null, display name of variable using (nameof(strNull))

Code


string FORMAT_WORLD_LENGTH = "{0}:- Length is {1}";
string strNull = null;

Console.WriteLine
            (
                  FORMAT_WORLD_LENGTH
                , strNull ?? ( "Variable " + nameof(strNull) )
                , strNull?.Length ?? -1
            ); 
        

Output

Output – Text
      
Variable strNull:- Length is 0
        
Output – Image

Summary

Please sanitize your variables.

At basic, see if they are nulls, empty, or zero.

Available Options includes:-

  1. Nullability Checks
    • Explicit
      • Equality Operator ( == )
        • example
          • if variableA == null
      • Inequality Operator ( != )
        • example
          • if variableA != null
    • Implicit
      • Null Conditional Method ( ?. )
        • example
          • strNull?.Length
      • Null Conditional Method ( ?. ) and null-coalescing operator ( ?? )
        • example
          • strNull?.Length ?? –1

 

References

  1. Microsoft
    • Learn > .NET > C# guide > Language reference > Operators and expressions
      • ?? and ??= operators (C# reference)
        Link
      • Equality Operator
        Link
      • Member access operators and expressions (C# reference)
        Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s