Difference between "?." and "."


#1

07

“Cleaning” my script I found that I wrote both

Sanderling?.MemoryMeasurementParsed?.Value?.blabla...

and sometimes

Sanderling.MemoryMeasurementParsed?.Value?.blabla... 

Both of the 2 syntaxes are working very well, so I wondered if there is a strong difference between them…?


#2

?. is used to ask in data structures, . is used to access directly.

For example Sanderling.waitmeasurements () is correct, because anytime the function is there.
but to descend into structure to blabla, is better to ask , because the structure can be null ( you dont have the value there , or you are between dock/undock)
Tests the value of the left-hand operand for null before performing a member access ( ?. ) or index ( ?[] ) operation; returns null if the left-hand operand evaluates to null .

These operators help you write less code to handle null checks, especially for descending into data structures.
next, the “.”
Use . to form a qualified name to access a type within a namespace, as the following code shows:

System.Collections.Generic.IEnumerable<int> numbers = new int[] { 1, 2, 3 };

Use the using directive to make the use of qualified names optional.

  • Use . to access type members, static and non-static, as the following code shows:
var constants = new List<double>();
constants.Add(Math.PI);
constants.Add(Math.E);

more about operators :slight_smile:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operator


#3

Thanks, I didn’t find that microsoft page when I made my researchs… :sweat:


#4

? is used for nullable elements, means you’ll receive NULL if something is NULL instead of FALSE in case of just .
and you can’t use ? on elements which are not nullable … but in general it’s doing almost the same