Skip to content

Use conditional access instead of accessing multiple times #1488

@RalfKoban

Description

@RalfKoban

If you have code such as following one, the code is difficult to read and not so performant:

if (a != null && a.b != null && a.b.c != null && a.b.c[0] != null)
{
    return a.b.c[0];
}

Instead, it should be refactored to something like:

var element = a?.b?.c?[0];
if (element != null)
{
    return element;
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    To do

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions