C #: Function in function possible?

Is it possible to declare a method in another method in C #?

For example, for example:

void OuterMethod() { int anything = 1; InnedMethod(); void InnerMethod() { int PlitschPlatsch = 2; } } 
+43
methods c #
May 04 '11 at 13:40
source share
8 answers

Not directly, no. C # does not support internal methods. It supports anonymous methods, although it may serve the same purpose.

 void OuterMethod() { int anything = 1; Action InnedMethod = () => { int PlitschPlatsch = 2; }; InnedMethod(); } 
+43
May 4 '11 at
source share

UPDATE: C # 7 added local functions ( https://docs.microsoft.com/en-us/dotnet/articles/csharp/csharp-7#local-functions )

 void OuterMethod() { int foo = 1; InnerMethod(); void InnerMethod() { int bar = 2; foo += bar } } 

In versions C # to C # 7, you can declare Func or Action and get something like this:

 void OuterMethod() { int foo = 1; Action InnerMethod = () => { int bar = 2; foo += bar; } ; InnerMethod(); } 
+30
May 04 '11 at 13:42
source share

Yes, there are ways. With C # 3.0, you have a Func<T> type that does this.

For example, I wrote this yesterday:

  var image = Image.FromFile(_file); int height = image.Height; int width = image.Width; double tan = height*1.00/width; double angle = (180.0 * Math.Atan(tan) / Math.PI); var bitmap = new System.Drawing.Bitmap(image, width, height); var g = System.Drawing.Graphics.FromImage(bitmap); int fontsize = 26; // starting guess Font font = null; System.Drawing.SizeF size; Func<SizeF,double> angledWidth = new Func<SizeF,double>( (x) => { double z = x.Height * Math.Sin(angle) + x.Width * Math.Cos(angle); return z; }); // enlarge for width do { fontsize+=2; if (font != null) font.Dispose(); font = new Font("Arial", fontsize, System.Drawing.FontStyle.Bold); size = g.MeasureString(_text, font); } while (angledWidth(size)/0.85 < width); 

The goal was to add a watermark to the existing image. I would like to make the text size of the watermark approximately 85% of the width of the image. But I wanted to overlay the text of the watermark so that it was written at an angle. This showed that some trigger calculations based on angles were necessary, and I wanted to do a little work. Func perfect for this.

In the above code, the Func function (function) is defined, which takes SizeF and returns a double , for the actual width of the text when it is drawn at a given angle. This Func is a variable inside the function, and the variable itself contains the function (reference to a). Then I can call this "private" function within the scope where I defined it. Func has access to other variables that are defined before it in the runtime. Thus, the angle variable is available in the angledWidth() function.




If you want an invokable logic that returns void , you can use Action<T> in the same way..NET defines Func generators that take N arguments, so you can make them pretty complicated. Func is like a VB function or a C # method that returns a non-void; The action is similar to a VB Sub or a C # method that returns void.

+15
May 04 '11 at 13:45
source share

Five years have passed since this question was asked, and now C # 7 is coming.

It should include local functions and, apparently, has already been implemented.

Local functions (Proposal: # 259) [currently in a future branch]

https://github.com/dotnet/roslyn/issues/259

+8
Mar 21 '16 at 20:53 on
source share

The delegate keyword does this.

 void OuterMethod() { var InnerMethod = delegate() { int PlitschPlatsch = 2; }; int anything = 1; InnerMethod(); } 

Remember that when defining the scope, the PlitschPlatsch variable PlitschPlatsch not available outside of InnerMethod.

+3
May 4 '11 at 1:45 pm
source share

See anonymous methods . I think what you are looking for.

+1
May 04 '11 at 13:44
source share

If you are looking basically to "hide" a method that is just an "assistant" ... another option is to create your private assistant (s). Thus, they do not become part of the public interface of the class.

This has the advantage that the helper can be reused if necessary; but he / they will not be "internal" to the calling method.

0
May 04 '11 at 13:50
source share

Yes, you can create a method inside a method

 void OuterMethod(){ int anything = 1; InnedMethod(); void InnerMethod() { int PlitschPlatsch = 2; }} 

this is absolutely correct syntax, and the compiler will not be through any error, but the volume of the internal method is only inside the external method, which cannot be called by the internal method from outside the external method. I have successfully completed this work. :)

0
Aug 02 '17 at 9:32 on
source share



All Articles