Method inside method

I am creating a C # library with some reusable code and trying to create a method inside a method. I have a method like this:

public static void Method1() { // Code } 

What I would like to do is:

 public static void Method1() { public static void Method2() { } public static void Method3() { } } 

Then I could choose either Method1.Method2 or Method1.Method3 . Obviously, the compiler is not happy with this, any help is greatly appreciated. Thank.

+42
methods c #
Nov 15 '11 at 10:41
source share
10 answers

This answer was written before the release of C # 7. With C # 7 you can write local methods.

No, you cannot do this. You can create a nested class:

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

Then you called:

 ContainingClass.NestedClass.Method2(); 

or

 ContainingClass.NestedClass.Method3(); 

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

Can you tell us more about what you are trying to achieve? There may be a better approach.

+37
Nov 15 '11 at 10:44
source share

If by a nested method you mean a method that can only be called in this method (for example, in Delphi), you can use delegates.

 public static void Method1() { var method2 = new Action(() => { /* action body */ } ); var method3 = new Action(() => { /* action body */ } ); //call them like normal methods method2(); method3(); //if you want an argument var actionWithArgument = new Action<int>(i => { Console.WriteLine(i); }); actionWithArgument(5); //if you want to return something var function = new Func<int, int>(i => { return i++; }); int test = function(6); } 
+82
Nov 15 2018-11-11T00:
source share

Yes, when C# 7.0 is released, Local Features will allow you to do this. You can use the method inside the method:

 public int GetName(int userId) { int GetFamilyName(int id) { return User.FamilyName; } string firstName = User.FirstName; var fullName = firstName + GetFamilyName(userId); return fullName; } 
+29
Aug 22 '16 at 15:10
source share

You can define delegates in your method with full code and call them if you want.

 public class MyMethods { public void Method1() { // defining your methods Action method1 = new Action( () => { Console.WriteLine("I am method 1"); Thread.Sleep(100); var b = 3.14; Console.WriteLine(b); } ); Action<int> method2 = new Action<int>( a => { Console.WriteLine("I am method 2"); Console.WriteLine(a); } ); Func<int, bool> method3 = new Func<int, bool>( a => { Console.WriteLine("I am a function"); return a > 10; } ); // calling your methods method1.Invoke(); method2.Invoke(10); method3.Invoke(5); } } 

There is always an alternative to using a nested class inside a class that will not be visible from the outside and will call its methods, for example:

 public class SuperClass { internal static class HelperClass { internal static void Method2() {} } public void Method1 () { HelperClass.Method2(); } } 
+24
Nov 15 '11 at 10:47
source share

With C # 7.0 you can do this:

  public static void SlimShady() { void Hi([CallerMemberName] string name = null) { Console.WriteLine($"Hi! My name is {name}"); } Hi(); } 

This is called local functions , this is exactly what you were looking for.

I gave an example from here , but further informative can be found here and here .

+5
Apr 13 '17 at 19:06 on
source share

Why aren't you using classes?

 public static class Helper { public static string MethodA() { return "A"; } public static string MethodA() { return "A"; } } 

Now you can get the MethodA method via

 Helper.MethodA(); 
+3
Nov 15 '11 at 10:45
source share

Yours almost there

 public static void Method1() 

it should be

 public static class Method1{} 
+2
Nov 15 '11 at 10:44
source share

Don't you want to use a nested class?

However, you do not seem to respect the Single Responsibility Principle , because you want one method to perform multiple operations at the same time.

+1
Nov 15 '11 at 10:48
source share

Old thread, but C # has a concept of nested functions

  Func<int> getCalcFunction(int total, bool useAddition) { int overallValue = 0; if (useAddition) { Func<int> incrementer = new Func<int>(() => { overallValue += total; return overallValue; }); return incrementer; } else { Func<int> decrementer = new Func<int>(() => { overallValue -= total; return overallValue; }); return decrementer; } } private void CalcTotals() { Func<int> decrem = getCalcFunction(30, false); int a = decrem(); //result = -30 a = decrem(); //result = -60 Func<int> increm = getCalcFunction(30, true); int b = increm(); //result = 30 b = increm(); //result = 60 } 
+1
Nov 30 '17 at 19:35
source share

Why don't you just run the method in another

public void M1 () {DO STUFF}

public void M1 () {DO STUFF M1 (); }

-2
May 30 '14 at 23:29
source share



All Articles