Question request syntax when calling a method

What does it indicate ? in the following c # code?

 var handler = CallBack; handler?.Invoke(); 

I read what before type can I use ? to indicate that it is a type with a null value. Does it do the same?

+8
c #
source share
1 answer

This is C # 6 code using a null conditional operator indicating that this code will not throw a NullReferenceException if the handler is null:

 Delegate handler = null; handler?.Invoke(); 

which do not allow you to record null checks that you would need to do in previous versions of C #:

 Delegate handler = null; if (handler != null) { handler.Invoke(); } 
+14
source share

All Articles