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
3
votes
2 answers

Anonymous methods cast as pointers

can anyone explain why the code below fails? type TIDEThemeObserverFunc = reference to procedure(foo: integer); var fObserverFuncs: TList function RegisterEventObserver(aObserverFunc: TIDEThemeObserverFunc): Pointer; begin …
utku_karatas
  • 6,163
  • 4
  • 40
  • 52
3
votes
5 answers

Assign value to Var in C# using try catch

I want to do something like this in C#. I think this is possible using Delegates or Anonymous Methods. I tried but I couldn't do it. Need help. SomeType someVariable = try { return getVariableOfSomeType(); …
rak
  • 304
  • 2
  • 12
3
votes
2 answers

Construct a function at runtime in C#

Lambda expressions are evaluated at compile time, so the below code will not generate a 100 different functions. Is there a simple mechanism to achieve the mentioned effect? I realize this isn't very efficient performance wise. List actions…
Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
3
votes
4 answers

Passing Delegate object to method with Func<> parameter

I have a method Foo4 that accepts a parameter of the type Func<>. If I pass a parameter of anonymous type , I get no error. But if I create and pass an object of the type 'delegate' that references to a Method with correct signature, I get compiler…
pradeeptp
  • 2,131
  • 6
  • 29
  • 39
3
votes
1 answer

Replacing anonymous methods with expression tree

If I want to replace this anonymous method: Func f = delegate(int i) { return i + 1; }; with an expression tree, it would like this: ParameterExpression i = Expression.Parameter(typeof(int), "i"); Expression one =…
Martin Mulder
  • 12,642
  • 3
  • 25
  • 54
3
votes
1 answer

Serializing a list of anonymous delegates

This question may be very similar to my one, but I cannot see the answer I need in it. I have a class, called CASM, that has a List. I want to serialize this class (using the BinaryFormatter or something similar). This class and all classes…
Callum Rogers
  • 15,630
  • 17
  • 67
  • 90
3
votes
1 answer

source code of anonymous method

How get a source code of anonymous method? For example: Func func = (() => DateTime.Now.Seconds % 2 == 0); Console.WriteLine(GetSourceCode(func)); // must: DateTime.Now.Seconds % 2 == 0 String GetSourceCode(Func f) - ???
Horev Ivan
  • 270
  • 3
  • 9
3
votes
8 answers

Anonymous methods/functions: a fundamental feature or a violation of OO principles?

Is the recent movement towards anonymous methods/functions by mainstream languages like perl and C# something important, or a weird feature that violates OO principles? Are recent libraries like the most recent version of Intel's Thread Building…
RD1
  • 3,305
  • 19
  • 28
3
votes
6 answers

Terminology when copying the captured variable for a lambda/ anon-method

I translated this code(it has bad side effect that it just capture the outer variable): foreach (TaskPluginInfo tpi in Values) { GenerateMenu(menuStrip, tpi.MenuTree, tpi.MenuText, delegate { tpi.ShowTask() }); } To this…
Hao
  • 8,047
  • 18
  • 63
  • 92
2
votes
6 answers

Closures for anonymous methods in C# 3.0

Why do closures exist for anonymous methods? Why not just pass state into the method without the overhead of a new class being generated with the closure variables being copied in? Isn't this just a throwback to "making everything global?" …
Jason Watts
  • 1,086
  • 5
  • 13
2
votes
1 answer

Disposing Class that Subscribe to an Event

I have a System.Windows.Forms.Form like this: public class MainForm : Form { int _processProgress; public int ProcessProgress { get { return _processProgress; } set { _processProgress = value; …
John Isaiah Carmona
  • 5,260
  • 10
  • 45
  • 79
2
votes
1 answer

event handler for WebBrowser control onpropertychange events - sender and e objects are null

In C#, I am running the WebBrowser (WB) control in a server-side thread and want to monitor (listen) for "onpropertychange" events. I can successfully attach a .NET delegate signature method that is executed when a property changes, but the sender…
2
votes
2 answers

Use Func in Ilist, why lambda expression?

I have a IList of Client type. I would need to iterate through it and return an element that matches some condition. I wanted to use "smarter" way than foreach so I tried Single method, however I am not sure why this works and if it can be done…
Petr Had
  • 137
  • 1
  • 6
2
votes
2 answers

Problem declaring an anonymous method with vb.net Action(Of T) and lambda

Imports System.Reflection Public Class Test Private Field As String End Class Module Module1 Sub Main() Dim field = GetType(Test).GetField("Field", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance) Dim…
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
2
votes
1 answer

Parameterless anonymous method can be assigned to parametered delegate

class Program{ static void Main(){ test11 jhbee = Program.test; //error test11 yep = delegate { }; //no error } static void test() { } } delegate void test11(int r); So I have the delegate test11 which returns…