Unused Private Method
ID |
csharp.unused_private_method |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
CSharp |
Tags |
CWE:561, code-style, unused-code |
Description
Reports private methods that are never called within their declaring type. A private method
is only reachable from inside its own type, so a method with no call sites is dead code that
can be removed.
Rationale
Dead private methods accumulate during refactors and obscure the type’s real surface. Removing
them shrinks the type and removes code that no one tests or maintains. Methods carrying an
attribute are excluded, because a framework may invoke them reflectively
({@code [EventHandler]}, {@code [Test]}), and partial methods are excluded because their call
sites may live in another file.
public class Worker
{
public void Run()
{
Step(); // calls the helper
}
private void Step() // OK — called by Run
{
Log("step");
}
private void Log(string m) { } // OK — called by Step
private int Orphan() // FLAW — never called
{
return 0;
}
}
Remediation
Delete the unused private method. If it was meant to be part of the public surface, give it the right accessibility; if a framework invokes it by reflection, add the attribute that documents that.