0

I just discovered that one can, in C#, declare a local function not just at the top level scope of the method but also in nested scopes in the method. I was wondering if this was widely know, I searched and didn't see it mentioned any where. What are you thoughts? Being used a bit to Common Lisp it feels good to be able to declare functions within nested contexts inside of a method.

My actual use case was that I had a big foreach going over a set of values, then there were several foreaches within the parent foreach that processed some forms of different types and matched for the current value of the parent foreach.

private void localMethod(){
guard clauses
init code
  foreach(var data in datalist){
    foreach(var control in CheckBoxesList)
    {
      if(data.prop == control.prop){
        do some specific stuff,
        localCommonLogic(control)
      }
    }
    foreach(var control in DropDownList)
    {
      if(data.prop == control.prop){
        do some specific stuff,
        localCommonLogic(control)
      }
    }
    void localCommonLogic(control)
    {
      Sett common logic on "control" that does not care about the specific type of list.
      "data" in in scope when this local function is declared here
    }
  }
}
  • Well, this is a feature introduces in C#7 for a reason. So asking if it is well known is like asking "do you knoe the worlöd is a potato"? Asking why this feature exists is something you should ask the language-designers at Microsoft. Alternativly have a look at the [docs](https://learn.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/local-functions) – MakePeaceGreatAgain Jan 24 '19 at 10:34
  • So what is the purpose of this question? A discussion abiout the uses of that feature? Which would be off-topic as opinion-based here. – MakePeaceGreatAgain Jan 24 '19 at 10:36
  • *"not just at the top level scope of the method but also in nested scopes in the method"* - cool finding, keep searching and keep us informed (sarcasm). I didn't know you can do it, but I noticed what I have to declare local functions **after** some `var` if I want to capture it (tooltip in VS tells which variables are captured). Scoping is good if it works so. Not sure how is it useful though. I'd ignore it unless you face an issue. – Sinatr Jan 24 '19 at 10:39
  • [See also](https://asizikov.github.io/2016/04/15/thoughts-on-local-functions/). This discusses the feature with links to the original design document. It's a (relatively) recent feature. For performance concerns, always bear [this](https://ericlippert.com/2012/12/17/performance-rant/) in mind, as well as Knuth's old adage about premature optimization. Unless you already know this function is a bottleneck, worrying about performance from declaring a function this way or that is definitely premature. – Jeroen Mostert Jan 24 '19 at 10:40
  • I'm not asking about the main feature, but that you can declare it in a nested scope of a method. My questions are about code style and if this affects performance negatively. Also no where in the docs does it say that you can do this. – HeinzGuderian Jan 24 '19 at 10:41
  • Okay, let me express differently. If we´d allowed this klind of question, how would you accept **the right** answer here? What *is* right? What is whrong in this scenario? It´s complteley opinion-based weather you consider this a good idea or not. Fact is that feature exists for a reason. However only yourself knows if that reason applies for you. – MakePeaceGreatAgain Jan 24 '19 at 10:44
  • In my linked article of MSDN its clearly shown that you can do it, isn´t it? – MakePeaceGreatAgain Jan 24 '19 at 10:44
  • 1
    Prior to C#7 this was possible using Action/Fun delegates - I have used this to add "local methods" that are only used in a particular method (e.g. mathematical function with repeated calculation within the expression). As far as your question is concerned - that gets into subjectivity which is off limits for SO. – PaulF Jan 24 '19 at 10:45
  • Without any testing I will assume that performance is relativly the same, did you notice any performance issue ? compare it to delegate and func to have reference point. – Drag and Drop Jan 24 '19 at 10:46
  • HimBromBeere: I don't see any example of it. Could you quote? – HeinzGuderian Jan 24 '19 at 10:47
  • In the link I provided have a look at the "local function syntax"-section. "The following example defines a local function named AppendPathSeparator that is private to a method named GetText:" – MakePeaceGreatAgain Jan 24 '19 at 10:51
  • And how does that show that you can in fact declare the local function within a nested codeblock of the method GetText? – HeinzGuderian Jan 24 '19 at 10:55

0 Answers0