Skip to content

C# Feature - Local functions

Local functions let you define a named helper inside a method, which keeps implementation details close to the calling code.

C# 7.0 (2017)

private bool IsValid(string candidate) => candidate.Length >= 3;
void Process(string value)
{
if (!IsValid(value)) return;
bool IsValid(string candidate) => candidate.Length >= 3;
}
  • Prefer a local function when the helper needs a name and belongs only to one outer method.
  • Use static local functions when no outer variables are needed.