Background
In this post we will cover the Null-Conditional Operator ( ?. ).
Series
Here are other posts that covers .Net Operators:-
- .Net:- Operator – The null-coalescing operator ??
Date Published:- 2022-November-8th
Link
Code
Code – HelloWorld
Code – HelloWorld
Outline
- Get String Length
- 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
- Get String Length
- 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
- Condition
- Checks string to determine if it is null
- The check is performed using the != operator
- 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
- Use ?.
- strNull?.Length
- The ?. means do not call the length method if the variable is null
- strNull?.Length
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
- Use ?.
- strNull?.Length
- The ?. means do not call the length method if the variable is null
- strNull?.Length
- 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))
- strNull?.Length ?? -1
- ??
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:-
- Nullability Checks
- Explicit
- Equality Operator ( == )
- example
- if variableA == null
- example
- Inequality Operator ( != )
- example
- if variableA != null
- example
- Equality Operator ( == )
- Implicit
- Null Conditional Method ( ?. )
- example
- strNull?.Length
- example
- Null Conditional Method ( ?. ) and null-coalescing operator ( ?? )
- example
- strNull?.Length ?? –1
- example
- Null Conditional Method ( ?. )
- Explicit