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
1 answer

How to do this in VB 2010 (C# to VB conversion)

I would like to have the following to be translated to VB 2010 (with advanced syntaxes) _domainContext.SubmitChanges( submitOperation => { _domainContext.Load( _domainContext.GetCustomersQuery(), …
user203687
  • 6,875
  • 12
  • 53
  • 85
1
vote
2 answers

Class assignment of anonymous function to property: calling assigned function from instance fails

I'm trying to run the $greeter function property of the $greeter instance of the Greeter class. I've read the answers from this related post but could not get them to work (the post also mentions _call, traits, stdClass, returning a function from a…
John Sonderson
  • 3,238
  • 6
  • 31
  • 45
1
vote
2 answers

Anonymous methods Scope c#

I wanted to know that where can we define the anonymous method (anonymous functions and lambda statement) because on some websites its written only in function and in some it is written that we can call it in class level scope.
user1955449
1
vote
1 answer

Is the state parameter useful when using a anonymous method in a callback?

I am using callbacks with the SynchronizationContext object to update client UIs from a WCF service. The code pattern I use is the following: public void SetResults(string callId, IEnumerable results) { uiSyncContext.Post(new…
Larry
  • 17,605
  • 9
  • 77
  • 106
1
vote
2 answers

How do I invoke a MethodInfo that was created from an anonymous method?

In a previous question, I asked how to get a MethodInfo from an Action delegate. This Action delegate was created anonymously (from a Lambda). The problem I'm having now is that I can't invoke the MethodInfo, because it requires an object to which…
Michael Meadows
  • 27,796
  • 4
  • 47
  • 63
1
vote
2 answers

c# inline switch in format string

I would like to format phrase and make endings according to number of items. string s = string.Format("There are {0} items, bla bla {1}", itemsCnt, () => {switch(itemsCnt) { case 0: return "make some..."; case 1: case 2: …
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
1
vote
2 answers

Working with methods that return anonymous methods

If I have a class like this: public class SomeClass { public Action SomeAction { get; set; } public SomeClass() { SomeAction = GetSomeAnonymousMethod(); } private Action GetSomeAnonymousMethod() { …
Kyle Baran
  • 1,793
  • 2
  • 15
  • 30
1
vote
2 answers

Action vs anonymous method question

I had a question answered which raised another one, why following does not work? I do not understand it. The compiler says: Cannot convert anonymous method do string. But why? public List list = new List(); private void…
Petr
  • 7,787
  • 14
  • 44
  • 53
1
vote
1 answer

Why is the captured parameter reset in this anonymous method?

The following code is based on this article: http://blog.barrkel.com/2010/01/using-anonymous-methods-in-method.html. When the event handler code inside the anonymous procedure is fired (upon changing a row in the grid), the first 'if' reports that…
boggy
  • 3,674
  • 3
  • 33
  • 56
1
vote
3 answers

How to get single column with LINQ in anonymous method

How to get single column with anonymous method using linq expression. Here's my code and it doesn't work: public IEnumerable GetPropertyValues(string propName) where T : class { return base.Query().AsEnumerable() .Where(x…
derodevil
  • 811
  • 1
  • 11
  • 37
1
vote
5 answers

Why can't I use break in a while loop in an anonymous method?

Why can't I use a break; statement in a while loop, whilst in an anonymous method? I was working on the piece of code (below), when I got this error: "Control cannot leave the body of an anonymous method or lambda expression". Thankfully I can…
Sam
  • 7,252
  • 16
  • 46
  • 65
1
vote
1 answer

In WebForms, why does my anonymous event handler not get called when added after OnLoad?

I have an ASP.NET WebForms page with several buttons added programmatically like this: private void AddExportButton(Control control, Action clickAction) { LinkButton exportButton = new LinkButton { Text = "Export", …
Marcel
  • 15,039
  • 20
  • 92
  • 150
1
vote
2 answers

Call Anonymous binded delegate method of radiobutton CheckedChanged

I have a dynamically generated radio button with anonymous delegate declared as in private void SetFieldDependency(DocumentSimpleFieldDetailDto obj, Table table, RadioButton ctrlExtended, Panel pnl) { if (this.ListOfDependentFields !=…
Kamran Shahid
  • 3,954
  • 5
  • 48
  • 93
1
vote
2 answers

ASP.NET Delegates and Expressions -Information Request

Based on my understanding , i interpret the meaning of Func delegate as follows.Please correct them as and when it is needed. Declaration : Func dg ; 1. Could i interpret it as "a Delegate pointing to a method that returns an …
user192332
1
vote
2 answers

Anonymous procedure PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following: := ( ; not null range default character

here's the code: http://sqlfiddle.com/#!4/ee7da/4247 CREATE TABLE supportContacts ( id int primary key, type varchar2(20), details varchar2(40) ) / INSERT INTO supportContacts (id, type, details) VALUES (1, 'Email',…
Kokizzu
  • 24,974
  • 37
  • 137
  • 233