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

C#.NetWpfEventsCompiler Warnings

C# Problem Overview


For example, I get this compiler warning,

> The event 'Company.SomeControl.SearchClick' is never used.

But I know that it's used because commenting it out throws me like 20 new warnings of XAML pages that are trying to use this event!

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

C# Solutions


Solution 1 - C#

This appears to be http://msdn.microsoft.com/en-us/library/858x0ycc.aspx">warning 67 and can thus be suppressed with:

#pragma warning disable 67

Don't forget to restore it as soon as possible (after the event declaration) with:

#pragma warning restore 67

However, I'd check again and make sure you're raising the event somewhere, not just subscribing to it. The fact that the compiler spits out 20 warnings and not 20 errors when you comment out the event is also suspicious...

There's also https://docs.microsoft.com/en-us/archive/blogs/trevor/c-warning-cs0067-the-event-event-is-never-used">an interesting article about this warning and specifically how it applies to interfaces; there's a good suggestion on how to deal with "unused" events. The important parts are:

>The right answer is to be explicit about what you expect from the event, which in this case, is nothing: > > > public event EventHandler Unimportant > { > add { } > remove { } > } > > This will cleanly suppress the warning, as well as the extra compiler-generated implementation of a normal event. And as another added benefit, it prompts one to think about whether this do-nothing implementation is really the best implementation. For instance, if the event isn't so much unimportant as it is unsupported, such that clients that do rely on the functionality are likely to fail without it, it might be better to explicitly indicate the lack of support and fail fast by throwing an exception: > > public event EventHandler Unsupported > { > add { throw new NotSupportedException(); } > remove { } > } > > Of course, an interface that can be usefully implemented without some parts of its functionality is sometimes an indication that the interface is not optimally cohesive and should be split into separate interfaces.

Solution 2 - C#

If you are forced to implement an event from an interface, that your implementation doesn't need you can do the following to avoid the warning.

public event EventHandler CanExecuteChanged { add{} remove{} }

Solution 3 - C#

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

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

As a variant on this, you can also just leave the add and remove methods empty to silently ignore subscriptions on the event.

The best solution is to refactor the code, perhaps pull the declaration of the event to the implementor if possible.

As a last resort, you can also disable the warning like so

#pragma warning disable 67
public event RoutedEventHandler SearchClick;
#pragma warning restore 67

Solution 4 - C#

You can also do the following:

public event EventHandler MyEvent = delegate {}

Solution 5 - C#

The compiler is apparently not aware that it's being used in XAML code. Try suppressing the warning in your event definition.

Also, make sure you're actually raising the event somewhere.

Solution 6 - C#

You can supress individual warnings.

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

In this case, CS0219 is the warning regarding variables being assigned but not used. You can either use the /nowarn:0219 flag, or add the error number in the properties pane for the project (under "Build", remember to remove the leading CS). Keep in mind the supresses all warnings of this class.

Solution 7 - C#

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

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  ...
  <NoWarn>67</NoWarn>
</PropertyGroup>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjedmaoView Question on Stackoverflow
Solution 1 - C#lc.View Answer on Stackoverflow
Solution 2 - C#Adam MillsView Answer on Stackoverflow
Solution 3 - C#vidstigeView Answer on Stackoverflow
Solution 4 - C#GrantAView Answer on Stackoverflow
Solution 5 - C#SLaksView Answer on Stackoverflow
Solution 6 - C#SvendView Answer on Stackoverflow
Solution 7 - C#SimonView Answer on Stackoverflow