Skip to content

Files

Latest commit

 

History

History
31 lines (22 loc) · 845 Bytes

File metadata and controls

31 lines (22 loc) · 845 Bytes

Pattern: Missing use of nameof

Issue: -

Description

nameof improves code maintainability in cases where the parameter may be renamed in the future, but the string literal is mistakenly not renamed. By using nameof, the symbol will be renamed when the parameter is renamed through a refactoring operation.

Example of incorrect code:

public Book(string title)
{
    // Violates rule CA1507
    Title = title ?? throw new ArgumentNullException("title", "All books must have a title.");
}

Example of correct code:

public Book(string title)
{
    // Resolves rule CA1507 violation
    Title = title ?? throw new ArgumentNullException(nameof(title), "All books must have a title.");
}

Further Reading