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
}
}
}