In Delphi, you can define functions within functions, example :
function Foo : integer;
var myvar : integer;
function Foo1;
begin
myvar := 42;
end;
begin
result := myvar;
end;
This returns 42 as expected because Foo1 has access to Foo’s myvar.
Is there any equivalent in C#?
Yes, there are many ways to do this. One way is to declare Func or Action delegates as follows:
The shorthand declaration (
=>) is commonly used in Linq. See lambdas.There are many generic
FuncandActiondelegates declared in the BCL (or whatever it’s called nowdays) to allow for all but the stupidest length parameter lists. You could always declare your own generic delegates if you need more parameters.