This question has already been answered:

In c # 7.0, nested functions have appeared. Actually the question is, and when is the use of this syntax justified? I understand that this is syntactic sugar, but IMHO and without them used to live well. To emphasize that this function is called in one place? Well I do not know...

Reported as a duplicate by Grundy , MihailPw , VladD. c # Jun 20 '17 at 6:40 pm

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    Nobody forbids to produce private functions that are needed in one place and then get confused in them. Markers are all. - vitidev
  • @vitidev, well, I do not know. For me, and in the nested functions can be confused. Moreover, nesting can be any. I also noticed, whether a bug, or a feature, that it is impossible to see a variable from an external function that was not passed as a parameter in a local function in the debugger. - iluxa1810
  • Or is this one even better: Why do we really need local functions? - Grundy
  • Now you can write type void w<T>(T t)=>Console.Write(t); and if console output is used at least in several places, win a couple of characters: w("Hello"); ))) - Andrey NOP
  • 2
    @FoggyFinder: And even before this was implemented in a procedural Pascal. - VladD

1 answer 1

To emphasize that this function is called in one place?

Not necessary. It can be executed many times in the method in which it is nested. Just now, such "locally executed" functions can not be moved to the class. As a result, the class will be cleaner and more readable.

By the way, the nested function has access to the local variables of the method:

 var number = 23; void DoAction() { Console.WriteLine(number); // 23 } DoAction(); 

Also found the benefit of using nested functions in event subscriptions. Let's give an example of how it looked before:

 EventHandler createdEventHandler = null; createdEventHandler += (sender, e) => { something.Created -= createdEventHandler; ... }; something.Created += createdEventHandler; 

And now with the help of nested functions:

 void OnCreated(object sender, EventArgs e) { something.Created -= OnCreated; ... } something.Created += OnCreated; 

What looks more readable?


Found another beautiful example posted by VladD :

 void SortBy<T>(List<T> list, Func<List, double> expr, bool ascending) { int comparerAscending(T t1, T t2) { return expr(t1).Compare(expr(t2)); } int comparerDescending(T t1, T t2) { return expr(t2).Compare(expr(t1)); } list.Sort(ascending ? comparerAscending : comparerDescending); } 
  • Do you have local variables from the external function displayed in the debugger when you enter the local one? For some reason, I swear at the context. - iluxa1810
  • The example with events somehow looks tight, especially if you remove the initialization with null - Grundy
  • one
    @Grundy and you cannot get rid of initialization with null, when you need to unsubscribe from this event when it fires (the variable has not yet been created). - MihailPw