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
Post a Comment