Skip to content

Latest commit

 

History

History
43 lines (32 loc) · 597 Bytes

GCop172.md

File metadata and controls

43 lines (32 loc) · 597 Bytes

GCop 172

"Remove the check for null. Instead use NullableObject?.Invoke()"

Rule description

In C#, from version 6, the ?. expression can be used to simplify the code.

Example 1

Delegate handler = null;
...
if (handler != null)
{
    handler.Invoke();
}

should be 🡻

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

Example 2

public void OnXYZ(SomeEventArgs e)
{
    var evt = XYZ;
    if (evt != null)
        evt(sender, e);
}

should be 🡻

public void OnXYZ(SomeEventArgs e) => XYZ?.Invoke(sender, e);