This question raises the question of planned innovations in c # version 7.
In particular, I was interested in the so-called local functions .
The @VladD response has an example of how a local function can be used:
IEnumerable<int> GetOdd(IEnumerable<int> s) { if (s == null) throw new ArgumentNullException(); // обратите внимание, `Inner` без параметров IEnumerable<int> Inner() { foreach (var v in s) if (v % 2 != 0) yield return v; } return Inner(); } but it seems to me that this example is slightly strained , in this case it would probably be possible to do without the Inner() function.
I also do not understand the following statement:
Regarding local functions, it seems to me that, on the contrary, often private functions of classes are used as a crutch in the absence of local functions. Often, a helper from a single function that has no value inside a class is taken to a private function. The local function is the more correct path for such functions.
I code in my code using separate methods based on the following rules:
- Functionality that can be reused;
- Functionalization in a separate method to maintain the harmony of the function;
- Other
For example:
public IEnumerable<string> GetPhonesForNotice(requestId) { var requestState = GetStateOfRequest(int requestId); switch(requestState) { case "Открыта": { return getRecipientsForNewRequest(int requestId); break; } case "Закрыта": { return getRecipientsForClosedRequest(int requestId); break; } } } private IEnumerable<string> getRecipientsForNewRequest(int requestId) { } private IEnumerable<string> getRecipientsForClosedRequest(int requestId) { } If I understood correctly, then in the new version of the function language getRecipientsForNewRequest (int requestId) , getRecipientsForClosedRequest (int requestId) can be implemented inside the main function GetPhonesForNotice (int requestId) but I don’t understand what is better / better than the current option?