Questions tagged [anonymous-methods]

An anonymous method is a procedure or function that does not have a name associated with it.

An anonymous method is a procedure or function that does not have a name associated with it. An anonymous method treats a block of code as an entity that can be assigned to a variable or used as a parameter to a method. In addition, an anonymous method can refer to variables and bind values to the variables in the context in which the method is defined. They are similar to the construct of closures defined in some languages.

An anonymous method in C# is a way to pass a code block as a delegate parameter.

320 questions
1
vote
2 answers

How to create anonymous object from T?

I have GetEntities method in EF context. In some cases I don't want to load all properties of entities in memory. I want to load only 'selected' properties. I'm using anonymous object for getting only special properties. For example, I have Product…
Dilshod K
  • 2,924
  • 1
  • 13
  • 46
1
vote
2 answers

Anonymous method should return a string by condition

I'm trying to understand writing anonymous Methods in C#. But having trouble to achieve a success. Please have a look at my example. I'm trying to fill a property named Value by given conditions. When I would write a private helper method which…
Sum1Unknown
  • 1,032
  • 1
  • 9
  • 14
1
vote
1 answer

How to subscribe to an event with a delegate

I would like to subscribe to an event so that when the event fires I can execute a delegate or anonymous function. Subscribing to events with methods is easy I can just type the method name, this works fine: UnityEngine.UI.Toggle…
1
vote
1 answer

Constructor parameter naming for clarity with passing in anonymous methods

I'm interested in the readability of my code when passing anonymous methods into delegate parameters: var touchListener = new TouchListener( down:(v, e) => { //Handle the down…
1
vote
1 answer

How can I make this anonymous function into a named function so I can move it to an external JS file for possible reuse

If I double posted this question, I'm sorry. I did not see it pop up under my questions that I'd asked in my profile, so I figured it didn't actually post. Anyway. I found the main logic for this code somewhere on stack overflow and modified it…
1
vote
1 answer

c# unsubscribe anonym method with extra parameters

I add images in PictureBox to a TableLayoutPanel and writing text on images with anonym methods, like this: private void AddPictureWithText(string text, int textX, int textY, int col, int row) { var picBox = new PictureBox() …
koviroli
  • 1,422
  • 1
  • 15
  • 27
1
vote
1 answer

Why does the line inside method of argument defined method anonymous inner class work?

InterfaceInAbstractClass.java public abstract class InterfaceInAbstractClass { public interface Inter{ void interface_method(); } public void interface_abstract_class_method(Inter in){ …
1
vote
1 answer

How can I extract the "real" target from an Event Handler that was defined anonymously?

Follow up to my question here: According to a comment made - The compiler creates a class member with a fictitious name and attaches it just as you would attach a declared method. I do not fully comprehend what this means but I can verify that…
Will
  • 3,413
  • 7
  • 50
  • 107
1
vote
1 answer

What kind of metadata is generated by anonymous methods? And is there a way to remove it?

I'm using the tool MapFileStats to inspect generated map files from delphi. I found that anonymous methods generate some kind of metadata, which doesn't seem to be related with RTTI. What kind of metadata is it? It would be nice to remove it because…
ventiseis
  • 3,029
  • 11
  • 32
  • 49
1
vote
2 answers

why are lambdas & anonymous methods not allowed on the left side of the is or as operator?

Lambdas are not allowed on the left side of the is or as operator. MSDN A clear explanation with an real example would be appreciated?
Amen Jlili
  • 1,884
  • 4
  • 28
  • 51
1
vote
2 answers

Anonymous Methods too much of a good thing? (C#)

Greetings, I'm working in a code base that uses alot of anonymous methods, where the anonymous methods are chaining other anonymus methods that call the same thing the first one calls. main() { anonymous1(); } anonymous1() { // call anonymous2…
Ka0s_Dem0n
  • 13
  • 2
1
vote
4 answers

Why I cannot use the anonymous method like that?

Why can't I have this? I mean it would spare a delegate declaration: int X=delegate int(){...}; I know it can be done this way: delegate int IntDelegate(); ... IntDelegate del=delegate(){return 25;}; int X=del();
Thomas
  • 2,575
  • 9
  • 31
  • 41
1
vote
1 answer

Anonymous methods and with some constant arguments

I have a function FUNC1(int a) and a function FUNC2(int a, int b). I have a delegate with a type of void() (no arguments). I want to have 2 variables. When I call one like this: VAR1() then FUNC1(4) will run. and VAR2 that will run FUNC2(2,9). I…
RexMan85
  • 59
  • 1
  • 1
  • 6
1
vote
2 answers

Linq Query Expanding

I have a string array named Products & I am working on Anonymous Methods (Lambda Exprs.) & Linq. So first I wrote; var resultSet1 = from p in products where p.StartsWith("M") orderby p descending …
mimozz
  • 94
  • 1
  • 9
1
vote
2 answers

Scope of variables inside anonymous functions in C#

I have a doubt in scope of varibles inside anonymous functions in C#. Consider the program below: delegate void OtherDel(int x); public static void Main() { OtherDel del2; { int y = 4; …
Vinod
  • 4,138
  • 11
  • 49
  • 65