Questions tagged [anonymous-delegates]

39 questions
22
votes
4 answers

Anonymous delegate as function parameter

I'm trying to pass parameter, which is anonymous delegate (no input parameters, no return value), to function. Something like this: private function DoSomething(delegate cmd) { cmd(); } Then, I'm want to use this function to call function in…
No1Lives4Ever
  • 6,430
  • 19
  • 77
  • 140
10
votes
2 answers

Dispatcher.Invoke with anonymous delegate works in Silverlight but not WPF

In Silverlight 4 I have a custom service class which has an asynchronous Completed event. Inside the Completed event I take the returned data and invoke a populate method via something like this: private void service_Completed(object sender,…
10
votes
2 answers

type arguments can't be inferred from the usage for higher-order function

I have the following higher-order function: public static Func Not(Func otherFunc) { return arg => !otherFunc(arg); } And trying to call it like that: var isValidStr = LinqUtils.Not(string.IsNullOrWhiteSpace); Compiler…
6
votes
2 answers

Why is casting to a generic type slower than an explicit cast in C#?

I'm building a message dispatch map in C# and mostly just playing around with some different approaches. I am curious about a performance difference I am measuring, but it's not obvious why from looking at the IL. The message map: delegate void…
6
votes
3 answers

Passing in an anonymous delegate to a thread...why does this work?

In my program, we split up a large amount of data that needs to be looked over across four threads. Thread one = new Thread(delegate() { NewMethod(recordsSplitIntoQuarters[0], param2, param3, param4, param5); }); Thread two = new Thread(delegate()…
5
votes
5 answers

Anonymous Delegates and generic Lists in C#

Can you explain me code below : private static List _Posts; public static Post GetPost(Guid id) { return _Posts.Find(delegate(Post p) { return p.Id == id; }); } What is the point to find an object in a generic list by…
Canavar
  • 47,715
  • 17
  • 91
  • 122
4
votes
1 answer

Anonymous delegate not using new local for every iteration when data on local stackalloc

When using anonymous delegates in C# the CLR will generate a copy of the local (e.g. variables in the current scope) on the heap for used variables. Such a local will be put onto the heap for every declared variable of the current scope. You can see…
Matthias
  • 948
  • 1
  • 6
  • 25
4
votes
2 answers

Is it possible to get the method body (text) of an Action?

I have a circumstance where I have enqueued a number of Action objects and I have a threadpool working through each Action. However, if the application shuts down before the queue is empty, I would like to log what is remaining in the queue before…
Mark Avenius
  • 13,679
  • 6
  • 42
  • 50
4
votes
3 answers

"dynamic" for anonymous delegates?

I wonder if there is a possibility to make the "dynamic" type for variables work for anonymous delegates. I've tried the following: dynamic v = delegate() { }; But then I got the following error message: Cannot convert anonymous method to type…
Van Coding
  • 24,244
  • 24
  • 88
  • 132
3
votes
3 answers

Can I return a reference to new object instance from a C# delegate?

I'm learning/experimenting with some functional patterns within C# and I've hit a bump I can't quite explain. I'm sure it's a simple answer (I hope) but I'm struggling to see it. Likely has to do with closures, etc and my inability to get…
robcom88
  • 63
  • 1
  • 6
2
votes
2 answers

C# anonymus delegates efficiency/safety

I have progress form which delegate: // in ProgressForm thread public delegate void executeMethod(object parameters); private executeMethod method; public object parameters; // ... private void ProgressForm_Shown(object…
Przemysław Michalski
  • 9,627
  • 7
  • 31
  • 37
2
votes
2 answers

Please help me understand anonymous delegates?

I've downloaded the VCSharpSample pack from Microsoft and started reading on Anonymous Delegates. I can more or less understand what the code is doing, but I don't understand the reason behind it. Maybe if you gave me some examples where it would…
Sergio Tapia
  • 40,006
  • 76
  • 183
  • 254
2
votes
2 answers

how to use anonymous generic delegate in C# 2.0

I have a class called NTree: class NTree { delegate bool TreeVisitor(T nodeData); public NTree(T data) { this.data = data; children = new List>(); _stopTraverse = false; } ... …
char m
  • 7,840
  • 14
  • 68
  • 117
2
votes
6 answers

lambda expressions in C#?

I'm rather new to these could someone explain the significance (of the following code) or perhaps give a link to some useful information on lambda expressions? I encounter the following code in a test and I am wondering why someone would do…
Matt
  • 303
  • 1
  • 2
  • 6
2
votes
1 answer

NCalc creating custom functions in vb.net

What is the equivalent code for creating a custom function in vb2010 instead of c#? Expression e = new Expression("SecretOperation(3, 6)"); e.EvaluateFunction += delegate(string name, FunctionArgs args) { if (name ==…
1
2 3