Posts Tagged ‘delegates’

How many methods does this interface have?

April 17, 2010 Leave a comment

How many methods are defined in this interface?

public interface IFoo
    void Bar<T>(T arg);

Read more…

That Unfortunate Interaction Between Multicast Delegates and Contravariance in C# 4.0

March 1, 2010 Leave a comment

About seven months ago, a little while after VS2010 Beta 1 came out, I was thinking about events, and to what extent the typical eventing pattern in .NET is necessary or unnecessary. Would it be okay to use Action as the delegate type, for example?
Read more…

Categories: C# 4.0, delegates Tags: ,

More on delegates vs interfaces

October 29, 2009 1 comment

A commenter on yesterday’s post asked about the lack of Dispose. Here’s a way of doing it:
Read more…

Categories: C#, delegates, functional Tags: , ,

Further Muddying the ForEach Waters

February 3, 2009 3 comments

ForEach is regularly proposed as a missing feature in the BCL but has been rejected in the past apparently because it wouldn’t be "functional" enough, doesn’t return anything so calls cannot be chained together in a LINQ-like pipeline, and so on. So here’s a different approach that is undeniably functional, and does support chaining together.

Read more…

Categories: C#, delegates, foreach Tags: , ,

The classic delegate/foreach interaction bug (and a solution)

June 13, 2008 2 comments

Update (2012-10-05): This whole issue has now been fixed in C# 5! The compiler just copies the loop variable for you, if you capture it in a closure. This is actually a breaking change, but a perfectly sensible one. Note that this only affects foreach. The plain for loop is unaffected.

Update (2011-06-17): Since I posted this I’ve regularly been told that there’s another solution where you declare a local variable inside the loop and copy the loop variable into it. Thanks, but I know that already! That’s the first solution everyone learns. The idea here was to suggest an alternative solution that works more elegantly, without adding extra clutter.

If you’ve used C# anonymous delegates or lambdas for a while (or anonymous functions in JavaScript) you will have encountered this problem. You make a delegate for each item in a list, and they all seem to wind up referring to the last item on the list. FUME!

It’s just an unfortunate side effect of the way foreach works. It reuses the same loop variable each time around the loop, and so that loop variable is created outside the scope of the loop, and so all the delegates you create inside the loop are actually looking at the same variable, not their own local copies.

Read more…

Delegates are Immutable

May 11, 2008 4 comments

Suppose you see this in some code:

button.Click += new EventHandler(button_Click);

Naturally you would conclude that Click is an event and button_Click is a method name (formally described as a method group, because the name by itself identifies one or more methods with different parameters). After the above line of code has been executed, the method button_Click will run each time the Click event fires (presumably when the button is clicked).

Read more…

Categories: C#, delegates Tags: ,