Skip to content

Gendarme.Rules.BadPractice.AvoidNullCheckWithAsOperatorRule(git)

Sebastien Pouliot edited this page Mar 2, 2011 · 1 revision

AvoidNullCheckWithAsOperatorRule

Assembly: Gendarme.Rules.BadPractice
Version: git

Description

The rule will detect if a null check is done before using the as operator. This null check is not needed, a null instance will return null, and the code will need to deal with as returning a null value anyway.

Examples

Bad example:

public string AsString (object obj)
{
    return (o == null) ? null : o as string;
}

Good example:

public string AsString (object obj)
{
    return (o as string);
}

Source code

You can browse the latest source code of this rule on github.com

Clone this wiki locally