C# Feature - Local functions
Overview
Section titled “Overview”Local functions let you define a named helper inside a method, which keeps implementation details close to the calling code.
Introduced In
Section titled “Introduced In”C# 7.0 (2017)
Before
Section titled “Before”private bool IsValid(string candidate) => candidate.Length >= 3;void Process(string value){ if (!IsValid(value)) return;
bool IsValid(string candidate) => candidate.Length >= 3;}Gotchas & Best Practices
Section titled “Gotchas & Best Practices”- Prefer a local function when the helper needs a name and belongs only to one outer method.
- Use
staticlocal functions when no outer variables are needed.