anonymous-methods

Anonymous methods and Async I/O

Can anyone tell me if I'm likely to run into unintended behavior if I use anonymous methods with Async I/O? As an example: Action<Socket> acceptedHandler = DoAccept SocketAsyncEventArgs e = new SocketAsyncEventArgs(); e.Completed += ((sender, ea) => acceptedHandler(ea.AcceptSocket)); // Server is a Socket if (!Server.AcceptAsync(e)) ...

Why don't anonymous delegates/lambdas infer types on out/ref parameters?

Several C# questions on StackOverflow ask how to make anonymous delegates/lambdas with out or ref parameters. See, for example: Calling a method with ref or out parameters from an anonymous method Write a lambda or anonymous function that accepts an out parameter To do so, you just need to specify the type of the parameter, as in: p...

Replacing a regular method with an anonymous method in C#/LINQ

I have a LINQ query that looks like this: public IEnumerable<Foo> SelectFooBars() { return from f in foos join b in bars on f.BarId equals b.Id select AddMissingProp(f, b.MissingProp); } public void AddMissingProp(Foo foo, string missingProp) // substitute this...

Cannot use 'this' in member initializer?

Is this legal? Does it contain a hidden bug or flaw? Visual studio does not give any errors or warnings but ReSharper does: /// <summary> /// immutable tuple for two /// </summary> public class Pair<TValue1, TValue2> : Singleton<TValue1> { public TValue2 Value2 { get; private set; } public Pair(TValue1 value1, TValue2 value2, Fu...

C# Adding and Removing Anonymous Event Handler

Hello, I was wondering if this actually worked ? private void RegisterKeyChanged(T item) { item.OnKeyChanged += (o, k) => ChangeItemKey((T)o, k); } private void UnRegisterKeyChanged(T item) { item.OnKeyChanged -= (o, k) => ChangeItemKey((T)o, k); } how does the compiler know that the event handlers are ...

BackgroundWorker with anonymous methods ?

Hi, I'm gonna create a BackgroundWorker with an anonymous method. I've written the following code : BackgroundWorker bgw = new BackgroundWorker(); bgw.DoWork += new DoWorkEventHandler( () => { int i = 0; foreach (var item in query2) { .... .... } } ); But Delegate '...

Closures in C# event handler delegates?

I am coming from a functional-programming background at the moment, so forgive me if I do not understand closures in C#. I have the following code to dynamically generate Buttons that get anonymous event handlers: for (int i = 0; i < 7; i++) { Button newButton = new Button(); newButton.Text = "Click me!"; newButton.Click ...

Interfaces, Anonymous Methods and Memory Leaks

Hi, this is a constructed example. I don't want to post the original code here. I tried to extract the relevant parts though. I have an interface that manages a list of listeners. TListenerProc = reference to procedure (SomeInt : ISomeInterface); ISomeInterface = interface procedure AddListener (Proc : TListenerProc); end; No...

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 = reference to procedure(param: integer); var a: TProc; b: TMyProc; begin b := proc...

Anonymous methods cast as pointers

Hi all, can anyone explain why the code below fails? type TIDEThemeObserverFunc = reference to procedure(foo: integer); var fObserverFuncs: TList<TIDEThemeObserverFunc> function RegisterEventObserver(aObserverFunc: TIDEThemeObserverFunc): Pointer; begin fObserverFuncs.Add(aObserverFunc); Result := @aObserverFunc; // line below ...

C# to VB - How do I convert this anonymous method / lambda expression across?

How would you convert this to VB (using .NET 4.0 / VS2010) ? bw.DoWork += (o, args) => { Code Here }; I thought maybe like this: AddHandler bw.DoWork, Function(o, args) Code Here End Function But it says Function does not return a value on all code paths. Ideas? ...

Assigning property of anonymous type via anonymous method

I am new in the functional side of C#, sorry if the question is lame. Given the following WRONG code: var jobSummaries = from job in jobs where ... select new { ID = job.ID, Description = job.Description, Fi...

Action<T> vs anonymous method question

Hi, I had a question answered which raised another one, why following does not work? I do not understand it. The compiler says: Cannot convert anonymous method do string. But why? public List<string> list = new List<string>(); private void Form1_Load(object sender, EventArgs e) { a.IterateObjects(B); // w...

How to break WinDbg in an anonymous method?

Title kinda says it all. The usual SOS command !bpmd doesn't do a lot of good without a name. Some ideas I had: dump every method, then use !bpmd -md when you find the corresponding MethodDesc not practical in real world usage, from what I can tell. Even if I wrote a macro to limit the dump to anonymous types/methods, there's no ob...

How to identify anonymous methods in System.Reflection

How can you identify anonymous methods via reflection? ...

Ugly thing and advantage of anonymos method -C#

I was asked to explain the ugly thing and advantages of anonymous method. I explained possibly Ugly thing anonymous methods turning quickly into spaghetti code. Advantages We can produce thread safe code using anonymous method :Example static List<string> Names = new List<string>( new string[] { "Jon Skeet", "Marc Grav...

C# -Closure -Clarification

I am learning C#.Can I mean closure as a construct that can adopt the changes in the environment in which it is defined. Example : List<Person> gurus = new List<Person>() { new Person{id=1,Name="Jon Skeet"}, new Person{id=2,Name="Marc Gravell"}, new Person{id=3,N...

How do I invoke a MethodInfo that was created from an anonymous method?

In a previous question, I asked how to get a MethodInfo from an Action delegate. This Action delegate was created anonymously (from a Lambda). The problem I'm having now is that I can't invoke the MethodInfo, because it requires an object to which the MethodInfo belongs. In this case, since the delegates are anonymous, there is no own...

C# Anonymous method variable scope problem with IEnumerable<T>

Hi. I'm trying to iterate through all components and for those who implements ISupportsOpen allow to open a project. The problem is when the anonymous method is called, then the component variable is always the same element (as coming from the outer scope from IEnumerable) foreach (ISupportsOpen component in something.Site.Container.Com...

LINQ vs Lambda vs Anonymous Methods vs Delegates

Can anyone explain what are the LINQ, Lambda, Anonymous Methods, Delegates meant? How these 3 are different for each other? Was one replaceable for another? I didn't get any concrete answer when i did Googling ...