First of all, your understanding of delegates is at least naive. A delegate does not act as a pointer to a function. First of all, there are no pointers in .NET (unless we talk unsafe code), there are references which is not the same; and a delegate instance does hold a pointer to the method, but also a reference to the instance of the class/structure, "this". Also, a delegate instance is an instance of some class which carries a whole container of such "pointers" called invocation list.
This is explained in my article
Dynamic Method Dispatcher[
^].
Now, let's sort out the issues with "inner function". There are no inner methods in C#, but there is a way to mimic such thing using delegates. This is explained in my Tips & Tricks article
Hide Ad-hoc Methods Inside the Calling Method’s Body[
^].
[EDIT]
Using inner functions in general and their implementations in the form of
anonymous delegates opens up a way to such an interesting phenomenon as
closure.
Please see
http://en.wikipedia.org/wiki/Closure_(computer_science)[
^].
In my past solution, I provided my own code sample and some explanations:
Looping for creating new thread give some problem[
^].
[END EDIT]
Please look at the code samples in the article referenced above. There,
AdjustLayout
is you "outer method",
adjustMargins
is your inner method, and
AdjustMargins
in first sample is a "regular" (not inner) variant of the same method.
A variant with "inner method" cannot be faster then a "regular" approach, because with "inner method" approach you first create an instance of a delegate and only after that invoke (call) the delegate instance. It certainly have some overhead. I think the performance loss will be insignificant for most applications.
When you say "faster as comparison to if I don't call the inner function through delegate", you probably don't mean not calling it at all but calling a "regular" version of the same method, because of course not calling anything is always faster than calling something. :-)
You can easily test performance. Just use the class
System.Diagnostics.Stopwatch
, see MSDN help page and code sample:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[
^]. If you do, please share your observations.
—SA