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

Is there a way to accomplish the equivalent of passing an "event" by reference?

I put "event" in quotes because I realize that it's a bit of syntax sugar, rather than a true type. I have some events which are simply chained to matching events in another class. So when the event is raised, the passage is like Raiser -> Proxy ->…
Igby Largeman
  • 16,495
  • 3
  • 60
  • 86
2
votes
2 answers

converting Linq/Lambda expressions to anonymous methods

I usually get code samples that uses lambda expressions. I am stil using .net 2.0, and find it difficult to work with such code, for example foreach(var item in items) { var catCopy = item; foreach(var word in words) { var…
Smith
  • 5,765
  • 17
  • 102
  • 161
2
votes
2 answers

Passing in an anonymous method/function as a parameter in C#

I have a method that needs to conditionally execute a method, something like this: int MyMethod(Func someFunction) { if (_someConditionIsTrue) { return someFunction; } return 0; } I want to be able to pass a Linq query…
Jerad Rose
  • 15,235
  • 18
  • 82
  • 153
2
votes
1 answer

E2555 Cannot capture symbol 'Self'

When I run the following code I get E2555 Cannot capture symbol 'Self'. type TLookupTable = record FData: TArray; procedure ReverseLeftToRight; end; procedure TLookupTable.ReverseLeftToRight; begin Parallel.For(0,…
Johan
  • 74,508
  • 24
  • 191
  • 319
2
votes
1 answer

Invoke method with generic expression and action as parameters

I need to call a method that looks like this: public bool DoSomething(Expression> expression, Action action) where TEntity : class However TEntity is only known at runtime. I know how to Invoke the method like…
Max
  • 51
  • 5
2
votes
1 answer

Delphi: Force capture of "unused" variable for anonymous method

I have a variable in a procedure that I need to keep alive until an anonymous method in that procedure runs, but I don't use the variable in the anonymous method. Is there an idiomatic way to tell the compiler to capture the variable anyway? For…
Zoë Peterson
  • 13,094
  • 2
  • 44
  • 64
2
votes
1 answer

Implementation options of summing, averaging, concatenating, etc items in an IEnumerable

I'm looking for the shortest code to create methods to perform common operations on items in an IEnumerable. For example: public interface IPupil { string Name { get; set; } int Age { get; set; } } Summing a property - e.g. IPupil.Age in…
John Paul Jones
  • 689
  • 4
  • 10
  • 17
2
votes
1 answer

How to write into List (class var) in inner anonymous method?

The global idea: Get data from REST API (https://jsonplaceholder.typicode.com/users/1) and then add data to the ListView on my activity layout. I'm getting data in anonymous class(using retrofit lib), but how can i write it to the ArrayList users…
user8913773
2
votes
2 answers

C#: Partial methods and Action, non implemented body

I want to call a partial method from outside of the declaring class. This is not allowed as partial methods are implicitly private. You can not set a delegate to point to a partial so I am proposing the following: public partial class MyClass { …
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
2
votes
1 answer

Dynamically create properties in anonymous type

I want to create a new type using anonymous types. This is my code: var t = paramaterList.Select(x => x).ToArray().Select(item => new { item, }); I need to create properties for var t based on the name of…
John
  • 263
  • 1
  • 15
2
votes
5 answers

Anonymous Member Pattern Export/Import Function

I'm a bit confused. What is the purpose of the parameters when you initiate an anonymous member pattern designated below: (function () { })(); I understand that Parameter A is used to designate the scope of the function is…
chrisjlee
  • 21,691
  • 27
  • 82
  • 112
2
votes
3 answers

Create unnamed implicit class or function

Is it possible to create an unnamed implicit class or function in Scala? For example, if I have the following implicit class: implicit class ListIntExtras(list: List[Int]) { def average = list.sum / list.size } I would prefer to be able to define…
socom1880
  • 340
  • 3
  • 12
2
votes
4 answers

Trying to do a minor refactor using lambdas in Java

I am working on part of a program (concerning speech recognition and a remote control car), where the code transmit(XXXXX); disableAutoMode(); is repeated many times. For curiosity's sake I would like to convert this into a lambda function similar…
TheIronKnuckle
  • 7,224
  • 4
  • 33
  • 56
2
votes
1 answer

Is it not possible to use var parameters in a Delphi anonymous method?

Is it not possible to use var parameters in anonymous methods? The following example illustrates (SSCCE) the problem I faced: program Project2; {$APPTYPE CONSOLE} type TTextTransformProc = reference to procedure(var AText: string); procedure…
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
2
votes
3 answers

Is there a useful design pattern for chained asynchronous/event calls?

I'm currently having to integrate a lot of web service calls inside Silverlight that make calls similar to the code below. No user interaction should be possible until the loading of all 3 is complete. // In my view, having standard delegate methods…
Chris S
  • 64,770
  • 52
  • 221
  • 239