Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy would an empty delegate event handler cause a CA1061 warning?
    primarykey
    data
    text
    <p><strong>Update:</strong> This occurs when the Code Analysis option "Suppress results from generated code (managed only)" is turned off, and Rule Set is set to "Microsoft Basic Design Guideline Rules".</p> <p>On 2013-04-26, Microsoft confirmed this is a bug, but will not fix it in either this or the next version of Visual Studio.</p> <p><a href="http://connect.microsoft.com/VisualStudio/feedback/details/758403/certain-event-handlers-initialized-with-empty-delegates-triggering-ca1061-warning-in-code-analysis" rel="nofollow noreferrer">Link to MS Connect item</a></p> <p>We frequently initialize event handlers with an empty delegate to avoid the need for checking nulls. E.g.:</p> <pre><code>public EventHandler SomeEvent = delegate {}; </code></pre> <p>However, since starting to compile some of our code in Visual Studio 2012 (RTM), I'm noticing a lot of events in derived classes are now triggering <a href="http://msdn.microsoft.com/en-us/library/ms182143.aspx" rel="nofollow noreferrer">CA1601: Do not hide base class methods</a> warnings in Visual Studio 2012's Code Analysis.</p> <p>Here's a sample that will trigger the warning:</p> <pre><code>using System; using System.ComponentModel; [assembly: CLSCompliant( true )] namespace TestLibrary1 { public abstract class Class1 { public event PropertyChangedEventHandler PropertyChanged = delegate {}; } public class Class2 : Class1 { // this will cause a CA1061 warning public event EventHandler SelectionCancelled = delegate { }; } public class Class3 : Class1 { // this will not cause a CA1061 warning public event EventHandler SelectionCancelled; } } </code></pre> <p>Note: In VS2012 the warning is triggered when compiled in either .NET 4.5 or .NET 4.0. The same sample does not trigger the warning in VS2010.</p> <p>Performance reasons aside, <strong>are there any legitimate reasons we shouldn't be initializing events with empty delegates?</strong> The default assumption is that it's probably just a quirk in the analysis in Visual Studio 2012. </p> <p>Here's the code analysis result for those that don't have access to VS2012 yet:</p> <blockquote> <p>CA1061 Do not hide base class methods Change or remove 'Class2.Class2()' because it hides a more specific base class method: 'Class1.Class1()'. TestLibrary1 Class1.cs 14</p> </blockquote> <p><strong>Addendum:</strong> I found that the option to "Suppress results from generated code" in the code analysis is turned off.</p> <p>Also, I found that this seems to occur when the event handler in the base type is both:</p> <ul> <li>a delegate other than EventHandler or EventHandler -and-</li> <li>events in both the base class and the derived class are initialized using an anonymous method or empty delegate (either inline or in the constructor).</li> </ul> <p>Of possible relevance: We're running Visual Studio 2012 RTM, which was installed in-place over the release candidate.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload