<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Virtual Events in C#</title><link>http://blogs.msdn.com/samng/archive/2007/11/26/virtual-events-in-c.aspx</link><description>Technorati Tags: Events One of the things that the language designers considered when designing the C# language was the ability to notify external callers of certain events happening. To solve this problem, they (surprise surprise) introduced the event</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Virtual Events in C#</title><link>http://blogs.msdn.com/samng/archive/2007/11/26/virtual-events-in-c.aspx#6537039</link><pubDate>Tue, 27 Nov 2007 02:41:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6537039</guid><dc:creator>DotNetKicks.com</dc:creator><description>&lt;p&gt;You've been kicked (a good thing) - Trackback from DotNetKicks.com&lt;/p&gt;
</description></item><item><title>re: Virtual Events in C#</title><link>http://blogs.msdn.com/samng/archive/2007/11/26/virtual-events-in-c.aspx#7094580</link><pubDate>Sun, 13 Jan 2008 06:22:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7094580</guid><dc:creator>PeterHuberSg</dc:creator><description>&lt;p&gt;I'm impressed how detailed you study the problem of virtual events and their possible shortcomings, although I doubt that many people would encounter that problem.&lt;/p&gt;
&lt;p&gt;A much more pressing design problem of the C# language is the lack of weak events. Presently, if an event listener registers an event handler with an event source, the event source will have a strong reference to the event listener. This can be a serious problem and many developers are even not aware of it. They create a new form, add the event handler, use the form and finally close the form. They assume the form will be garbage collected, but this is not the case, because they forgot to unregister the eventhandler and there is still a strong reference pointing to the form. Unfortunately, there are situation were it is not that easy to decide when to unregister from an event source. Just ask teh WPF team for examples.&lt;/p&gt;
&lt;p&gt;That the event listener should have the same life span as the event source is completely counter intuitive. And even if the developer is aware of that problem and wants to solve it, .NET doesn't offer him any help to do so. I spent weeks trying to get in run efficiently, writing a WeakEventManager as recommended in some blogs. Even Microsoft's own WPF team had to go this way and ended up with a horribly over complicated solution, the WeakEventManager, which can only be used for WPF applications. And what can the rest of us use ?&lt;/p&gt;
&lt;p&gt;It would be so easy to add a second kind of events, which use a weak reference pointer to the event listener. All that needs to be done is that the garbage collector consider the reference as weak.&lt;/p&gt;
&lt;p&gt;Trying to fix this design problem of C# is extremly hard to do for a developer and many highly qualified programmers have failed or come up with terribly complicated, inefficient solutions, like generating code during runtime.&lt;/p&gt;
&lt;p&gt;I'm sure you guys would get much more appreciation if you would provide a solution for weak events instead of virtual events.&lt;/p&gt;</description></item></channel></rss>