Does C # have a concept of methods inside methods?

I use javascript and I used functions inside functions a lot. I tried this in C #, but it seems like they don't exist. If I have the following:

public abc() { } 

How can I encode the d() method, which can be called from within the abc() method?

+6
source share
7 answers

I wouldn’t worry so much about restricting access to a method at the method level, but at a higher level, you can use private to restrict method access to this particular class.

Another alternative could be to use lambdas / anonymous methods or use C # 4.0, Action / Tasks to create them inside your method.

An example of an anonymous method using a delegate (C # 1/2/3/4) for your specific example (including I need an action that can take a string parameter and return a string?) Would be something like this

 delegate string MyDelegate(string); public void abc() { // Your code.. MyDelegate d = delegate(string a) { return a + "whatever"; }; var str = d("hello"); } 

.. using C # 3/4:

 public void abc() { // Your code.. Func<string, string> d = (a) => { return a + "whatever"; }; var str = d("hello"); } 

.. using a more ideal solution using the private method:

 private string d(string a) { return a + "whatever"; } public void abc() { // Your code.. var str = d("hello"); } 

Based on your comment for another answer: I just wanted to have this at the bottom of the method and then call it from earlier code.

This will not be possible, you will need to define a variable for your method using delegates or Actions , and therefore it must be fully initialized by the time you call it. Then you cannot determine this at the bottom of your method. A much better option would be to simply create a new private method in your class and call it.

+5
source

You cannot declare a method inside another method, but you can create anonymous functions inside methods:

 public void abc() { Action d = () => { ... }; // ... d(); } 

... that can only be called inside the method by the abc () method?

A method can only be called if you have a link to it. If you do not store the link elsewhere, you should be fine.


How to transfer and return a string to action?

Use Func instead of Action :

 Func<string, string> d = s => { return s + "foo"; }; 

The reason I would like to do this is to make my code more readable.

It’s good to try to make your code more readable, but I think this change will make it less readable. I suggest you use regular methods, not anonymous functions. You can make them private so that they cannot be called from outside your class.

+3
source

This is not a way to define classes, but you can do:

 public abc() { Action d = () => { // define your method }; d(); } 
+3
source

Yes, they are called delegates and anonymous methods.

Delegate signatures must be predefined outside the method for the body to be assigned, so it is not exactly like a function. First you declare a delegate:

 class MyClass { public delegate boolean Decider(string message); /* ... */ } 

And then in MyClass.MyMethod you can say Decider IsAllLowerCase = /* method name or anonymous method */; and then use it with var result = IsAllLowerCase(s); .

The good news is that .NET already has delegate definitions for most signatures that you might need. System.Action has different signatures for methods that return nothing, and System.Func for those that do.

As shown elsewhere,

 Action<int, string> a = (n, s) => { for(var i=0; i<n; i++) Console.WriteLine(s);}; 

Lets you call a( /* inputs */ ); as if it were a local variable. (stuff) => { code } is a "lambda expression" or an anonymous method, you can also just pass the method name (if the signature matches):

 Action<string> a = Console.WriteLine; 

If you want to return something, use Func :

 Func<bool, string> f = (b) => { return b.ToString(); }; 

Lets you call var result = f(b); in the same way.


As a note, delegates are a fun part of C # /. NET, but usually the way to control access is to make another method inside your class and declare it private. If your problem is with names, then you may need refactoring. For example, you can group methods in another class declared inside your source class (nested classes are supported) or completely move them to another class.

+2
source

You can use action delegates

 public abc() { Action action = () => { //Your code here } action(); } 

Edit: To pass parameter

 public abc() { Action <string>action = (str) => { //Your code here }; } action("hello"); 

Using Func to Return a Value

 public void abc() { Func<string, string> func = (str) => { return "You sent " + str; }; string str = func("hello"); } 
+2
source

Use action delegates. More effective than you.

 public abc() { Action <int> GetInt = (i) => { //Write code here Console.Writeline("Your integer is: {0}", i); }; GetInt(10); } 

Action is a delegate, so you can specify a parameter as a method, not a variable. The action delegate encapsulates a method that has no parameters and does not return a value. Check it out from MSDN.

+2
source

You can create a nested class:

 public class ContainingClass { public static class NestedClass { public static void Method2() { } public static void Method3() { } } } 

Then yu can call:

 ContainingClass.NestedClass.Method2(); 

or

 ContainingClass.NestedClass.Method3(); 

I would not recommend this. This is usually a bad idea to have public nested types.

+2
source

All Articles