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

Strange bug with anonymous methods in 'initialization' section

This unit fails to compile in XE2 Update 3 with error "Internal Error: SY6315". In XE there is no such problem. unit Test; interface uses SysUtils; var Proc: TProc; implementation initialization Proc := procedure var ByteArr:…
BofA
  • 823
  • 7
  • 16
7
votes
1 answer

How can I use anonymous methods in Free Pascal?

I tried to use Delphi's syntax for anonymous methods: type fun = reference to function(): Integer; Fpc shows a syntax error: Error: Identifier not found "reference" What's the Free Pascal equivalent to Delphi's anonymous methods, if any?
mcandre
  • 22,868
  • 20
  • 88
  • 147
7
votes
1 answer

Replace Property Getter/Setter with Reflection or similar (no 3rd party libraries) in c#

I've got a property contained in a class for example public class Greeter { private Hashtable _data; public string HelloPhrase { get; set; } public Greeter(data) { _data = data; } } What I would like to do is add an Attribute to…
Mike Miller
  • 16,195
  • 1
  • 20
  • 27
7
votes
1 answer

Optional Anonymous Method

I would like to expose a function that can take an optional anonymous method : type TParamsProc = reference to procedure(Params: TSQLParams); TFieldsProc = reference to procedure(Fields: TSQLResult); TDbController = class …
ZeDalaye
  • 689
  • 7
  • 17
7
votes
1 answer

Strange behaviour of TypeInfo by anonymous methods

For a piece of code that needs the type "family" of a generic type, I try to use the TypeInfo to retrieve the required information. class function GetTypeKind:TTypeKind; For most types I can figure this out. But the anonymous method type behaves…
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
7
votes
3 answers

Using MethodInfo.GetCurrentMethod() in anonymous methods

public static void Main(string[] args) { Action a = () => Console.WriteLine(MethodInfo.GetCurrentMethod().Name); a(); } This code will return an obscure string like so:
b__0. Is there a way of ignoring the anonymous methods and get a…
HuBeZa
  • 4,715
  • 3
  • 36
  • 58
7
votes
1 answer

(_) => DoWork(); How an underscore is valid as a anonymous delegate parameter?

In an excellent answer about starting a timer immediately, I could see the following code: timer.Elapsed += timer_Elapsed; ThreadPool.QueueUserWorkItem((_) => DoWork()); ... void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs…
Larry
  • 17,605
  • 9
  • 77
  • 106
7
votes
3 answers

How to identify anonymous methods in System.Reflection

How can you identify anonymous methods via reflection?
devlife
  • 15,275
  • 27
  • 77
  • 131
7
votes
2 answers

How do lambda expressions work internally?

While looking up the answer to this question: "Why is an out parameter not allowed within an anonymous method?" I've got a little lost about how do lambda expression and anonymous methods actually work. In the comments JaredPar states that "Imagine…
Jorge Córdoba
  • 51,063
  • 11
  • 80
  • 130
6
votes
5 answers

C# technique for generating anonymous delegates that share same closure variable

I have a situation where I need to generate a few similar anonymous delegates. Here's an example: public void Foo(AnotherType theObj) { var shared = (SomeType)null; theObj.LoadThing += () => { if(shared == null) …
FMM
  • 4,289
  • 1
  • 25
  • 44
6
votes
3 answers

Getting target of Action

I have created the fallowing Sample-Code: class Program { static void Main(string[] args) { var x = new ActionTestClass(); x.ActionTest(); var y = x.Act.Target; } } public class ActionTestClass { public Action…
BennoDual
  • 5,865
  • 15
  • 67
  • 153
6
votes
3 answers

How to avoid anonymous methods in "dynamic" event subscription?

How could I refactor the method private void ListenToPropertyChangedEvent(INotifyPropertyChanged source, string propertyName) { source.PropertyChanged += (o, e) => { if (e.PropertyName ==…
Jens
  • 25,229
  • 9
  • 75
  • 117
6
votes
2 answers

How to circumvent using an out parameter in an anonymous method block?

The following method does not compile. Visual Studio warns "An out parameter may not be used within an anonymous method". The WithReaderLock(Proc action) method takes a delegate void Proc(). public Boolean TryGetValue(TKey key, out TValue value) { …
Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
6
votes
6 answers

Declaring Func dynamically

Consider this: var propertyinfo = typeof(Customer).GetProperty(sortExpressionStr); Type orderType = propertyinfo.PropertyType; now I want to declare Func I know its not possible directly since ordertype is at runtime but is there…
Stacker
  • 8,157
  • 18
  • 73
  • 135
6
votes
2 answers

What problems are there using generics and anonymous methods in Delphi 2009?

I'd like to start using generics and anonymous method, mainly to learn what that's all about and why I would want to use them. Having Delphi 2009, I often read that generics and anonymous methods are not completely implemented or buggy, which was…
Holgerwa
  • 3,430
  • 9
  • 42
  • 50