What's new in C# 6.0: Null-conditional operators

One of the many new and nice C# 6.0 features is the Null-conditional operators. The example given in the linked presentation is the following:

You want to do something as simple as raise an event, e.g.

OnChanged(this, args);

To be on the null pointer-safe side, you will have to make this an ugly abomination like:

{
var onChanged = OnChanged;
if (onChanged != null)
{
onChanged(this, args);
}
}

With C# 6.0 you can perform those checks easily with the so called "Elvis"-operator "?.":

OnChanged?.Invoke(this, args);


Sources:

Comments

Popular posts from this blog

SQL Server Setup: Windows Firewall warning (Ports)

SQL Server 2014 Enterprise Edition: Server Setup: Feature Selection

How to read an assembly.dll.config