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

Cannot access to property of anonymous type in debug mode (VS2013)

In this sample console app: class Program { static void Main() { DoAsyncFoo(); Console.ReadKey(); } private static async void DoAsyncFoo() { var task = CollectStatsAsync(); dynamic foo = await…
skynyrd
  • 942
  • 4
  • 14
  • 34
6
votes
2 answers

What issue does "reference to" solve

On Chris's blog: http://delphihaven.wordpress.com/2011/07/14/weird-in-more-ways-than-one/ I found the following code type TLinkVisitor = reference to procedure(const Item: T); TDoubleLinked = record Prev: ^TDoubleLinked; Next:…
Johan
  • 74,508
  • 24
  • 191
  • 319
6
votes
1 answer

How does anonymous methods omit parameter list?

I was reading this in the MSDN documentation on Anonymous Methods (C# Programming Guide), but I do not understand the part about omitting the parameter list. It says: There is one case in which an anonymous method provides functionality not found…
Damian
  • 4,395
  • 4
  • 39
  • 67
6
votes
4 answers

Why variables have to be final in anonymous methods and class fields don't

If I had this anonymous method I should declare x variable as final. private void testMethod (ListField listField){ final ListLoader loader = new PagedListLoader(); listField.addListener(Events.Attach,…
Amin Abu-Taleb
  • 4,423
  • 6
  • 33
  • 50
6
votes
1 answer

Is it possible to define an anonymous selector in Objective-C?

I'd like to be able to define an inline anonymous selector that a selector wherever a selector is needed as an argument. Is this possible, or do I have to just suck it up and define a method? Background: In my iPhone application I need to update my…
Ben S
  • 68,394
  • 30
  • 171
  • 212
6
votes
2 answers

How can I capture variables by anonymous method when using it in OTL?

What I want to do: I have a few objects in a genric list. I want to capture each of this object in anonymous method and execute this method as a separate OTL Task. This is a simplified example: program Project51; {$APPTYPE CONSOLE} uses …
Wodzu
  • 6,932
  • 10
  • 65
  • 105
6
votes
2 answers

local variable scope in linq anonymous method ( closure)

What is the scope of local variable declared in Linq Query. I was writing following code static void Evaluate() { var listNumbers = Enumerable.Range(1, 10).Select(i => i); int i = 10; } Compiler flagged error on line int…
Tilak
  • 30,108
  • 19
  • 83
  • 131
5
votes
1 answer

How do I use the ValueTuple naming feature with anonymous methods?

I would like to use the naming feature of ValueTuple as follows: IEnumerable<(string, char, int)> valueTuples = new(string, char, int)[] { ("First", '1', 1), ("Second", '2', 2), ("Third", '3', 3) }; var…
mikevg
  • 53
  • 4
5
votes
2 answers

Why is one Func valid and the other (almost identical) not

private static Dictionary> _parseActions = new Dictionary> { { typeof(bool), value => {Convert.ToBoolean(value) ;}} }; The above gives an…
gingerbreadboy
  • 7,386
  • 5
  • 36
  • 62
5
votes
3 answers

Casting anonymous procedures in Delphi 2009

The following code (constructed only to demonstrate the problem) compiles and works in Delphi 2010. In Delphi 2009, compiler fails with "E2035 Not enough actual parameters". program Project50; {$APPTYPE CONSOLE} uses SysUtils; type TMyProc =…
gabr
  • 26,580
  • 9
  • 75
  • 141
5
votes
2 answers

How to implement a Comparator to compare names?

I need to Implement a static method in my class Student public static Comparator getCompByName() that returns a new comparator object for Student that compares 2 Students objects by the attribute 'name'. Iv created what I think is…
Reeggiie
  • 782
  • 7
  • 16
  • 36
5
votes
8 answers

Why does my attempt to trim strings in a List not appear to work?

I tried the following code in LINQPad and got the results given below: List listFromSplit = new List("a, b".Split(",".ToCharArray())).Dump(); listFromSplit.ForEach(delegate(string s) { s.Trim(); }); listFromSplit.Dump(); "a"…
rohancragg
  • 5,030
  • 5
  • 36
  • 47
5
votes
5 answers

How to return a generic list collection in C#?

I have some linq to sql method and when it does the query it returns some anonymous type. I want to return that anonymous type back to my service layer to do some logic and stuff on it. I don't know how to return it though. I thought I could do…
chobo2
  • 83,322
  • 195
  • 530
  • 832
5
votes
5 answers

Create anonymous method from a string in c#

is it possible to create an anonymous method in c# from a string? e.g. if I have a string "x + y * z" is it possible to turn this into some sort of method/lambda object that I can call with arbitrary x,y,z parameters?
toasteroven
  • 2,700
  • 3
  • 26
  • 35
5
votes
1 answer

What do permit anonymous parameterless delegate types to differ?

Having read in article "Anonymous Methods" (as part of the articles series "Delegates and Lambda Expressions in C# 3.0") the phrase: "Advanced Topic: Parameterless Anonymous Methods ... anonymous methods are allowed to omit the parameter list…
Fulproof
  • 4,466
  • 6
  • 30
  • 50