Local Functions vs. Private Methods in C#: When to Use Which
A practical comparison of C# local functions and private methods, covering scope, reuse, readability, and when each one is the right tool.
C# gives you two ways to pull logic out of a method body without exposing it publicly: a local function nested inside the method, or a private method at the class level. They can look interchangeable in simple cases, but they answer genuinely different questions, and picking the wrong one tends to show up later as either an overstuffed class or a method that’s harder to follow than it needs to be.
Local functions
A local function is defined within the body of another method, and can only be called from within that method’s scope.
When they fit:
- Encapsulating logic that’s only relevant to one method. If the helper logic has no reason to exist outside the method that uses it, keeping it local keeps the class free of clutter that only ever has one caller.
- Readability. Related logic stays physically together, instead of forcing a reader to jump elsewhere in the class to see what a small helper actually does.
- Avoiding parameter sprawl. Because a local function can close over variables from its containing method, it sidesteps the need to pass a long list of parameters the way a private method would require.
- Simplifying what would otherwise be a lambda. When logic destined for a delegate or lambda gets complex enough to want a name and a body of its own, a local function is often clearer than an inline lambda expression.
public void ExampleMethod()
{
void LocalFunction()
{
// Implementation of local function
}
// Use of local function
LocalFunction();
}
Private methods
A private method is defined at the class level with the private modifier, and can be called from anywhere within that class.
When they fit:
- Reuse across multiple methods. If more than one method in the class needs the same logic, a private method avoids duplicating it.
- Breaking up a method that’s grown too large. Splitting an oversized method into smaller private methods is a direct improvement to readability and maintainability.
- Modularity for future changes. Logic that might reasonably be needed elsewhere in the class later is more accessible as a private method than buried inside one specific method.
- Indirect testability. Private methods aren’t directly unit-testable, but organizing code into well-named private methods makes the public methods that call them easier to reason about and test.
public class ExampleClass
{
public void ExampleMethod()
{
// Use of private method
PrivateFunction();
}
private void PrivateFunction()
{
// Implementation of private function
}
}
The comparison, side by side
- Scope. Local functions are scoped to the single method they’re defined in; private methods are scoped to the whole class.
- Use case. Local functions suit small, method-specific tasks; private methods suit logic that’s genuinely reusable across the class.
- Readability and maintenance. Local functions keep a single method’s related logic together in one place; private methods keep a class modular and organized as a whole.
The short version
Reach for a local function when the logic exists purely to serve one method and has no business being called from anywhere else. Reach for a private method when the logic needs to be shared across multiple methods, or when a method has simply grown too large and needs to be broken apart at the class level. The scope of the logic’s usefulness is the whole decision — everything else follows from that.