How do I get rid of the "[some event] never used" compiler warning in Visual Studio?

For example, I get this compiler warning,

The Company.SomeControl.SearchClick event is never used.

But I know that it was used because when commenting on this, I get 20 new warnings about the XAML pages that are trying to use this event!

What gives? Is there any trick to get rid of this warning?

+71
c # events compiler-warnings wpf
Jul 07 '09 at 16:16
source share
7 answers

This appears to be warning 67 and can be suppressed with:

#pragma warning disable 67 

Remember to restore it as soon as possible (after the announcement of the event) with:

 #pragma warning restore 67 



Nevertheless, I would once again check and make sure that you are raising an event somewhere, and not just subscribing to it. The fact that the compiler generates 20 warnings, and not 20 errors when commenting on an event, is also suspicious ...

There is also an interesting article about this warning and, in particular, how it applies to interfaces; There is a good suggestion on how to deal with "unused" events. Unfortunately, this link seems (temporarily?) Dead now, but the important parts are:

The correct answer should be clear about what you expect from the event, which in this case means nothing:

 public event EventHandler Unimportant { add { } remove { } } 

This will purely suppress the warning, as well as the additional implementation of the regular event generated by the compiler. And, as another added benefit, it makes you wonder if this implementation, which does nothing, is really the best implementation. For example, if an event is not so important, because it is not supported, so clients who rely on functionality may fail without it, it might be better to explicitly indicate a lack of support and quickly fail, throwing an exception:

 public event EventHandler Unsupported { add { throw new NotSupportedException(); } remove { } } 

Of course, an interface that can be advantageously implemented without some parts of its functionality sometimes indicates that the interface is not optimally coordinated and should be divided into separate interfaces.

+108
Jul 07 '09 at 16:23
source share

If you are forced to inject an event from the interface that your implementation is not needed, you can do the following to avoid a warning.

 public event EventHandler CanExecuteChanged { add{} remove{} } 
+68
Sep 09 '10 at 8:50
source share

The second best way is imho to clearly indicate that an event is not supported by throwing an exception if someone tries to subscribe to it.

 public event RoutedEventHandler SearchClick { add { throw new NotSupportedException(); } remove { } } 

As an option to this, you can also simply leave the add and remove methods blank to silently ignore event subscriptions.

The best solution for code refactoring, perhaps, if possible attracts the event declaration to the developer.

As a last resort, you can also turn off the warning this way

 #pragma warning disable 67 public event RoutedEventHandler SearchClick; #pragma warning restore 67 
+13
Dec 03 '13 at 13:07 on
source share

The compiler does not seem to know that it is used in XAML code. Try suppressing the warning in your event definition.

Also, make sure you pick up the event somewhere.

+1
Jul 07 '09 at 16:22
source share

You can suppress individual warnings.

 \Program.cs(13,20): warning CS0219: The variable 'foo' is assigned but its value is never used 

In this case, CS0219 is a warning about the assignment of variables, but is not used. You can either use the / nowarn: 0219 flag, or add an error number in the properties area for the project (in the "Assembly" section, do not forget to remove the leading CS). Keep in mind that all warnings in this class are suppressed.

+1
Jul 07 '09 at 16:23
source share

Or you can add <NoWarn>67</NoWarn> to your project

 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> ... <NoWarn>67</NoWarn> </PropertyGroup> 
+1
Nov 05 '13 at 4:52
source share

You can also do the following:

 public event EventHandler MyEvent = delegate {} 
0
Aug 14 '18 at 21:16
source share



All Articles