I have been using javascript and I made a lot of use of functions inside of functions. I tried this in C# but it seems they don’t exist. If I have the following:
public abc() {
}
How can I code a method d() that can only be called
from inside the method the method abc() ?
I wouldn’t worry so much about the restriction of access to a method on the method level but more class level, you can use
privateto restrict access of the method to that specific class.Another alternative would be to use lambdas/anonymous methods, or if you’re using C# 4.0,
Action/Tasksto create them inside your method.An example of an anonymous method using a delegate (C# 1/2/3/4) for your specific example (incl. I need an action that can take a string parameter and return a string?) would be something like this:
.. using C# 3/4:
.. using a more ideal solution through
privatemethod:Based on your comment for another answer: I would just like to have this at the bottom of the method and then call it from some earlier code.
This won’t be possible, you would need to define a variable for your method using either delegates or
Actionsand so it would need to be fully initialised by time you call it. You wouldn’t then be able to define this at the bottom of your method. A much better option would be to simply create a newprivatemethod on your class and call that.