C# 6.0 Features Series
- How to try C# 6.0 and Rosyln?
- Getter-only (Read Only) Auto Properties in C# 6.0
- Lambda and Getter Only Auto-Properties in C# 6.0
- Initializers for Read-Only Auto Properties in C# 6.0
- Initializers via Expression Auto Properties in C# 6.0
- C# 6.0 – A field initializer cannot reference the non-static field, method, or property
- Lambda Expression for Function Members in C# 6.0
- Dictionary Initializers (Index Initializers) in C# 6.0
- Expression Bodies on Methods returning void in C# 6.0
- using keyword for static class in C# 6.0
- Unused namespaces in Different Color in Visual Studio 2015
- Null-Conditional Operator in C# 6.0
- Null-Conditional Operator and Delegates
- nameof Operator in C# 6.0
- Contextual Keywords in C#
- String Interpolation in C# 6.0
- Exception Filters in C# 6.0
- Await in Catch and finally block in C# 6.0
When you want to invoke an delegate , you might want to perform null check so that you wont get the null reference exception .
In C# 6.0 , we can use the Null-Conditional operator when invoking the delegate to perform the null check and do the invocation . The only catch is that , you cannot do the straightforward way of calling a delegate when followed by the ?. operator . You will need to use the Invoke method as shown below.
FirstNameChanged?.Invoke(this,args)
It is also said to be one of the easiest and the thread safe way to check for nulls before triggering an event.
1 Comment