Why is this an issue?

private or internal types or private members that are never executed or referenced are unused code: unnecessary, inoperative code that should be removed.

Cleaning out the unused code decreases the codebase size, making it easier to understand and preventing bugs from being introduced.

Redundant code is included in the compilation so it needs to be compiled as well. Due to this, removing it will reduce the compilation time and the project maintanace. It will also simplify the onboarding time for new joiners since they will not need to understand what it does and why it’s there.

Exceptions

This rule doesn’t raise issues on:

How to fix it

Code examples

Noncompliant code example

public class Foo
{
    private void UnusedPrivateMethod(){...} // Noncompliant, this private method is unused and can be removed.

    private class UnusedClass {...} // Noncompliant, unused private class that can be removed.
}

Compliant solution

public class Foo
{
    public Foo()
    {
        UsedPrivateMethod();
    }

    private void UsedPrivateMethod()
    {
        var c = new UsedClass();
    }

    private class UsedClass {...}
}

Resources

Documentation