anonymous-methods

How to subscribe to an event dynamically using anonymous methods?

I'm creating a number of UserControls dynamically using LoadControl(String) and want to subscribe to an event of each of them. All my controls inherits a common Interface that require an implementation of a common Event: public interface IObjectProcessor { event EventHandler<ObjectProcessedEventArgs> ObjectProcessed; } So I do next ...

Anonymous methods, scope, and serialization.

Let's say I have the following code: public class Foo { private int x; private int y; public Bar CreateBar() { return new Bar(x, () => y); } } [Serializable] public class Bar { private int a; private Func<int> b; public Bar(int a, Func<int> b) { this.a = a; this.b = b; }...

Can you use .net 3.5 Action or Func as Marshalled unmanaged delegates?

After reading Dynamically calling unmanaged dlls in .net I've been trying to modify the code to my liking. I made a class that implements idisposable to wrap load calls in and free them when needed. However I can't seem to figure out the syntax if it is possible to use anonymous delegates with it. var loaded=DynamicLibraryLoader.TryLo...

How do I Unregister 'anonymous' event handler

Say if I listen for an event: Subject.NewEvent += delegate(object sender, NewEventArgs e) { //some code }); Now how do I un-register this event? Or just allow the memory to leak? ...

C# - anonymous functions and event handlers question

Hi Guys, I have the following code: public List<IWFResourceInstance> FindStepsByType(IWFResource res) { List<IWFResourceInstance> retval = new List<IWFResourceInstance>(); this.FoundStep += delegate(object sender, WalkerStepEventArgs e) { if (e.Step.ResourceType =...

Implementing timeout for calling generic function with parameters.

I am trying to get my head around the use of the Action delegate type for use in forcing a timeout when methods called in a 3rd party COM dll hang up. After much searching I find that I can use Action<> or Func<> and pass up to 4 generic parameters depending on whether the method called returns a parameter or not. For this instance I wi...

Creating two delegate instances to the same anonymous method are not equal

Consider the following example code: static void Main(string[] args) { bool same = CreateDelegate(1) == CreateDelegate(1); } private static Action CreateDelegate(int x) { return delegate { int z = x; }; } You would imagine that the two delegate instances would compare to be equal, just as they would when using the good old name...

Anonymous methods/functions: a fundamental feature or a violation of OO principles?

Is the recent movement towards anonymous methods/functions by mainstream languages like perl and C# something important, or a weird feature that violates OO principles? Are recent libraries like the most recent version of Intel's Thread Building Blocks and Microsofts PPL and Linq that depend on such things a good thing, or not? Are lan...

PHP Import Foreign Class' Method into MyClass

Wondering if this is possible in PHP Land: Let's say I have a class as follows: class myClass{ var $myVar; ... myMethod(){ $this->myVar = 10; } } and another class: class anotherClass { ... addFive(){ $this->myVar += 5; } } The 'anotherClass' is 3500 lines long and I just want the single 'addFive' m...

Premature string destruction and how to avoid it?

Hi! I'm using Delphi 2009 and get some strange errors using the following code segment: var Str : AnsiString; CharPtr : PAnsiChar; ... CharPtr := PAnsiChar (Str); ExecuteInBackgroundThread ( procedure begin DoSomething (CharPtr); end); I'm guessing that the string is destructed when falling out of scope and under some ...

How do you use Func<> and Action<> when designing applications?

All the examples I can find about Func<> and Action<> are simple as in the one below where you see how they technically work but I would like to see them used in examples where they solve problems that previously could not be solved or could be solved only in a more complex way, i.e. I know how they work and I can see they are terse and ...

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? ...

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<int> dg ; 1. Could i interpret it as "a Delegate pointing to a method that returns an integer?". Declaration : Func<int,int> delg1=r=>2*r; *2. Could i interpret it as " 'r' is a la...

Serializing a list of anonymous delegates.

This question may be very similar to my one, but I cannot see the answer I need in it. I have a class, called CASM, that has a List<Action>. I want to serialize this class (using the BinaryFormatter or something similar). This class and all classes referenced in the Actions have got correct [Serializable] and [NonSerializable] attributes...

Why is an out parameter not allowed within an anonymous method?

This is not a dupe of Calling a method with ref or out parameters from an anonymous method I am wondering why out parameters are not allowed within anonymous methods. Not allowing ref parameters makes a bit more sense to me, but the out parameters, not as much. what are your thoughts on this ...

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 for instance that the out parameter referred to a local variable on the stack. The lambda...

Passing Delegate object to method with Func<> parameter.

I have a method Foo4 that accepts a parameter of the type Func<>. If I pass a parameter of anonymous type , I get no error. But if I create and pass an object of the type 'delegate' that references to a Method with correct signature, I get compiler error. I am not able to understand why I am getting error in this case. class Learn6 ...

ok, this worked. what is it exactly?

I just lifted this snippet from a website and it proved to be exactly the solution I needed for my particular problem. I have no idea what it is (particularly the delegate and return parts) and the source doesn't explain it. Hoping SO can enlighten me. myList.Sort( delegate(KeyValuePair<String, Int32> x, KeyValuePair<String, Int32>...

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 UI from another thread. To do this I use performSelectorOnMainThread:withObject:waitUnti...

How to return a generic list collection in C#?

Hi 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 this public List<T> thisIsAtest() { return query; } but I get this error Error...