Thursday, December 11, 2008

I think the best feature introduced in .NET 2.0 was anonymous methods in C#. Hows that for taking a stance!

I've just been reading an article about anonymous methods coming to VB in .NET 4.0 and while I think this is great news for VB developers its worth adding a note of caution due to one of the examples used. Generally anonymous methods and event handlers can cause unforseen issues due to the way anonymous methods are implemented under the covers (I'm assuming the VB implementation is broadly the same as the C# one). If you look at the code below:

class Program
{
    static void Main(string[] args)
    {
        Foo f = new Foo();

        f.ItHappened += delegate
        {
            Console.WriteLine("Foo happens");
        };

        f.ItHappened -= delegate
        {
            Console.WriteLine("Foo happens");
        };

        f.Bar();
    }
}

class Foo
{
    public event Action ItHappened;

    public void Bar()
    {
        if (ItHappened != null)
            ItHappened();
    }
}

The problem is the compiler generates two different real methods for the two anonymous methods. Therefore the unwiring of the handler fails. It comes down to this: if you wire up an event hander with an anonymous method, unless you store the delegate in a variable, there is no way to remove that event handler afterwards. There is a well known issue in .NET in terms of memory leaks with event handlers keeping alive objects that otherwise have independent lifetimes. You fix this issue by remembering to remove the event handler when the object is finished with. However, using anonymous methods (or lambdas for that matter) prevents you from removing the event handler and so this issue becomes unresolvable.

So my advice: even though anonymous methods are very powerful, generally use real methods for event handlers unless the lifetime of the event owner and the subscriber are inherently tied together.

Thursday, December 11, 2008 7:21:46 PM (GMT Standard Time, UTC+00:00)  #    Comments [23]Trackback
Friday, December 12, 2008 9:28:07 AM (GMT Standard Time, UTC+00:00)
This is not entirely true. There will be no memory leaks if you attach an anonymous method to an object and dereference that object because anonymous methods are either static or live in an object created simply for their sake.
This means that the problem is not that there will be memory leaks if you use anonymous methods to attach to events (they will die when the object raising the events is dereferenced), but rather that they will be attached to the event for the entire lifetime of the object. In 99.9% of the cases I've seen to date, this is hardly a problem.
Friday, December 12, 2008 5:22:23 PM (GMT Standard Time, UTC+00:00)
Defeatist!

It's entirely possible to unattach a handler implemented as an anonymous method. It's a little verbose but it's easily done:

Timer t = new Timer();
ElapsedEventHandler h = null;
h = delegate
{
t.Elapsed -= h;
t.Stop();
};
t.Elapsed += h;
t.Interval = 1000;
t.Start();

You simply have to store the method in a variable, and then you can unattach it later. And in this case, because I'm unattaching the handler inside the handler itself, I also had to initialize the variable to null to avoid a compiler error - C# appears not to understand that the field must have been initialized by the time the anonymous method runs.

So it's not quite accurate to say that use of anonymous methods prevents you from removing the handler.

Saturday, December 13, 2008 6:50:23 AM (GMT Standard Time, UTC+00:00)
Hi Omer

Thats not strictly true. If an anonymous method references member variables but not local variables the compiler generates an instance method on the class. Also if the anonymous method captures local state then, as you say, you get a new class - however this local state could be quite large. So if the hte event ownver and subscriber have independent lifetimes then the evet could quite easily keep significant object alive when they should otherwise have been garbage.
Richard
Saturday, December 13, 2008 6:55:39 AM (GMT Standard Time, UTC+00:00)
Hi Ian

yes you are absolutely right. If you store all your event handlers in variables you can indeed unsubscribe them. Unfortunately this is not a particularly useful pattern in general. Alsom although its a neat trick to unsubscribe in the event handler - the decision to rely on the event being raised so that you can unsubscribe is fairly dangerous. I have amended the post to satisfy your pendatry however :-P
Richard
Saturday, December 13, 2008 9:04:22 AM (GMT Standard Time, UTC+00:00)
Richard,

My point is that these are edge cases (I have yet to encounter one) and that your call to not use anonymous methods in event handlers is a bit too general.

(also, it would have been nice to know my email address would have been published before inputting it; that's just plain not nice of your blog)
Saturday, December 13, 2008 9:22:20 AM (GMT Standard Time, UTC+00:00)
Hi Omer,

my call was to not use them "unless the lifetime of the event owner and the subscriber are inherently tied together". In the most common cases teh lifetimes are tied together (controls on a window for example). Situations where they are not tied together anonymous methods can potentially cause bugs which are hard to track down.

Sorry that you didn't realise that your email address would be shown (although obfuscated to some degree from spam engines). I will see if Das Blog has some setting to hide them
Richard
Monday, June 28, 2010 5:31:49 AM (GMT Daylight Time, UTC+01:00)
You write articles, I read the original views of other authors related articles, but feel that you are the best. You know, and comparison of industry comprehensive knowledge very attract me. Hope you to write more and better articles sharing
Monday, June 28, 2010 5:32:43 AM (GMT Daylight Time, UTC+01:00)
flywowgold why is really good?is my website.
Tuesday, August 24, 2010 8:55:05 PM (GMT Daylight Time, UTC+01:00)
Do you recognize that it's high time to receive the <a href="http://bestfinance-blog.com/topics/mortgage-loans">mortgage loans</a>, which can help you.
Monday, September 06, 2010 1:39:13 AM (GMT Daylight Time, UTC+01:00)
Vielen Dank! Super Artikel! Wenn Sie Elektronik möchten,Können Sie im unser efox-shop suchen.wir bieten Videospiele Zubehör ,PSP Zubehör ,Nintendo Wii Zubehör usw.
welcome you to : http://www.efox-shop.com/
Die besten Dual SIM Handys, <a href="http://www.efox-shop.com/pc-videospiele-c-236.html/"> Videospiele Zubehör </a.>.
Vielen Dank! Super Artikel! Wenn Sie Elektronik möchten,Können Sie im unser efox-shop suchen.wir bieten NDSLZubehör , NDSL Zubehör , NDSi Zubehör , Nintendo DS Lite Zubehör AND Konsolen usw.
welcome you to : http://www.efox-shop.com/ Die besten Dual SIM Handys, <a href="www.efox-shop.com/pc-videospiele-ndslndsi-zubehör-c-236_391.html">NDSLZubehör</a>,
Vielen Dank! Super Artikel! Wenn Sie Elektronik möchten,Können Sie im unser efox-shop suchen.wir bieten Wii Zubehör kaufen AND PS2Zubehör ,Playstation 2-Spiele, diverses Zubehöre usw.
welcome you to : http://www.efox-shop.com/ Die besten Dual SIM Handys, <a href="www.efox-shop.com/pc-videospiele-sony-ps2-zubehör-c-236_398.html">PS2Zubehör</a.>,
Vielen Dank! Super Artikel! Wenn Sie Elektronik möchten,Können Sie im unser efox-shop suchen.wir bieten Autoteile, Auto Zubehör,Auto Schmuck ,Kfz Zubehör AND China GPS , Navigation Gerät AND Auto GPS Gerät kaufen AND Konsolen usw.
welcome you to : http://www.efox-shop.com/pc-videospiele-china-spiel-konsole-c-236_395.html Die besten Dual SIM Handys, <a href=" www.efox-shop.com/pc-videospiele-china-spiel-konsole-c-236_395.html "> Konsolen </a.>,
Friday, October 29, 2010 3:14:33 AM (GMT Daylight Time, UTC+01:00)
C# 2.0 provides a new feature called Anonymous Methods, which allow you to create inline un-named ( i.e. anonymous ) methods in your code, which can help increase the readability and maintainability of your applications by keeping the caller of the method and the method itself as close to one another as possible. This is akin to the best practice of keeping the declaration of a variable, for example, as close to the code that uses it.
Friday, January 28, 2011 7:19:10 AM (GMT Standard Time, UTC+00:00)
In www.mygoldseller.com, We only sell the top replica handbags, some of replica handbags are genuine leather handbags, yet the price is far lower than the authentic designers want you to pay. We lead you to a genuine pool of bags : collections in wide range: handbags, shoulder bags, Pouch, Purse,Tote bags,Backpacks, the hottest brands you can find like
Louis Vuitton : http://www.domainike.com
Dolce&Gabbana : http://www.mygoldseller.com
Polo : http://www.mygoldseller.com
Gucci : http://www.mygoldseller.com
Chloe : http://www.mygoldseller.com
Coach : http://www.domainike.com
Chanel : http://www.domainike.com
Juicy : http://www.mygoldseller.com
Prada : http://www.mygoldseller.com
Fendi : http://www.mygoldseller.com
Marc jacobs : http://www.mygoldseller.com
Ed hardy : http://www.mygoldseller.com
MiuMiu : http://www.mygoldseller.com
Jimmy choo : http://www.domainike.com
Versace : http://www.mygoldseller.com
Burberry : http://www.mygoldseller.com
Dooney&Bourke : http://www.mygoldseller.com
christian Audigier : http://www.mygoldseller.com
Saturday, March 19, 2011 5:42:55 AM (GMT Standard Time, UTC+00:00)
Welcome to http://www.wholesalebigmarkets.com/index.php, http://www.domainikes.com/index.php
we wholesale many kinds of brand products with high quality and low price.
LV handbags: http://www.wholesalebigmarkets.com/category-19-b0-Louis+Vuitton.html
D&G handbags: http://www.wholesalebigmarkets.com/category-5-b0-DG.html
Gucci handbags: http://www.wholesalebigmarkets.com/category-7-b0-GUCCI.html
Coach handbags: http://www.wholesalebigmarkets.com/category-9-b0-COACH.html
Chanel handbags: http://www.wholesalebigmarkets.com/category-10-b0-CHANEL.html
Juicy handbags: http://www.wholesalebigmarkets.com/category-11-b0-JUICY.html
Prada handbags: http://www.wholesalebigmarkets.com/category-12-b0-PRADA.html
Fendi handbags: http://www.wholesalebigmarkets.com/category-13-b0-FENDI.html
Ed hardy handbags: http://www.wholesalebigmarkets.com/category-14-b0-Ed+Hardy.html
Dior handbags: http://www.wholesalebigmarkets.com/category-539-b0-Dior.html
Burberry handbags: http://www.wholesalebigmarkets.com/category-18-b0-Burberry.html
Versace handbags: http://www.wholesalebigmarkets.com/category-17-b0-VERSACE.html
Brand designer women's handbags: http://www.wholesalebigmarkets.com/category-1-b0-Handbags++Bags.html
wholesale brand women's shoes: http://www.wholesalebigmarkets.com/category-24-b0-WomenShoes.html
cheap men's shoes: http://www.wholesalebigmarkets.com/category-25-b0-Mens+Shoes.html
fashion men's clothes:http://www.wholesalebigmarkets.com/category-352-b0-Mens+Clothing.html
beautiful women's clothes: http://www.wholesalebigmarkets.com/category-421-b0-Womens+Clothing.html
new fashion style jewelry: http://www.wholesalebigmarkets.com/category-516-b0-Jewerly.html
if you are sure to order we can give you the lowest price,hope we can have a chance to do business relationship with you.
looking forward to your kindly reply
have a nice shopping!
Friday, April 08, 2011 1:28:35 AM (GMT Daylight Time, UTC+01:00)
Set your own life more easy take the loans and everything you want.
Saturday, April 09, 2011 10:47:23 AM (GMT Daylight Time, UTC+01:00)
Thanks for sharing! it is good to encourage people to comment, not just reading. The only reason I writing blog rather than diary is because of the feedback.
Tuesday, May 17, 2011 3:45:32 PM (GMT Daylight Time, UTC+01:00)
Great post man! I always follow your blog because it is full of compelling information about various things. I like to read this post because I met so many new facts about it actually. It's really great that I noticed this post. Thank you very much again for sharing this informative article. Many thanks,
Tuesday, May 17, 2011 3:46:34 PM (GMT Daylight Time, UTC+01:00)
Great post man! I always follow your blog because it is full of compelling information about various things. I like to read this post because I met so many new facts about it actually. It's really great that I noticed this post. Thank you very much again for sharing this informative article. Many thanks,
Wednesday, July 06, 2011 1:26:22 AM (GMT Daylight Time, UTC+01:00)
I will tell my friends about this. I just bookmarked this site for future reference.
Sunday, January 29, 2012 11:19:06 AM (GMT Standard Time, UTC+00:00)
Smack-dab what I was looikng for-ty!
Monday, January 30, 2012 8:12:01 AM (GMT Standard Time, UTC+00:00)
3I9jAZ <a href="http://doucecknisny.com/">doucecknisny</a>
Monday, January 30, 2012 12:51:34 PM (GMT Standard Time, UTC+00:00)
t7MSQc , [url=http://uutddjznbpam.com/]uutddjznbpam[/url], [link=http://cdmeunlciwha.com/]cdmeunlciwha[/link], http://buczfhgtjpvd.com/
Wednesday, February 01, 2012 7:26:31 AM (GMT Standard Time, UTC+00:00)
aQWhsL <a href="http://rfrlejjokrnz.com/">rfrlejjokrnz</a>
Wednesday, February 01, 2012 12:28:47 PM (GMT Standard Time, UTC+00:00)
dO9pj4 , [url=http://eywmftcurxie.com/]eywmftcurxie[/url], [link=http://pitaxlgtwvjb.com/]pitaxlgtwvjb[/link], http://peflwtgzsruc.com/
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):