All rules
CA1822Performance Enabled by default: As suggestion MS default: Suggestion

Mark members as static

Mark members that do not access instance state as `static`.

Microsoft docs

Description

Members that do not use instance data can be marked static, which avoids passing the hidden this parameter.

Cause

A member that does not access instance data is not marked as static (Shared in Visual Basic).

Why it matters

Static members avoid an unnecessary this argument and make it clear the member does not depend on instance state.

How to fix violations

Mark the member as static (or Shared in Visual Basic) or use 'this'/'Me' in the method body, if appropriate.

Examples

Avoid
public int Square(int x) => x * x;
Prefer
public static int Square(int x) => x * x;

When to suppress

It's safe to suppress a warning from this rule in the following cases:

  • For previously shipped code for which the fix would be a breaking change.
  • For methods in classes that inherit from System.MarshalByRefObject. The methods in these classes shouldn't be marked as static, because the .NET remoting infrastructure uses instance dispatch to forward calls across AppDomain boundaries. Making such methods static can break remoting across AppDomain boundaries.
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0