== 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:

* empty constructors
* members with attributes
* `Main` method
* methods with event handler signature `void Foo(object, EventArgs)` that are declared in partial class
* empty serialization constructor on type with https://learn.microsoft.com/en-us/dotnet/api/system.serializableattribute[System.SerializableAttribute] attribute.
* internal members in assemblies that have a https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.internalsvisibletoattribute[System.Runtime.CompilerServices.InternalsVisibleToAttribute] attribute.

== How to fix it

=== Code examples

==== Noncompliant code example

[source,csharp,diff-id=1,diff-type=noncompliant]
----
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

[source,csharp,diff-id=1,diff-type=compliant]
----
public class Foo
{
    public Foo()
    {
        UsedPrivateMethod();
    }

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

    private class UsedClass {...}
}
----

== Resources

=== Documentation

* https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers[Access Modifiers (C# Programming Guide)]

include::rspecator.adoc[]
