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

前端 未结 7 1416
伪装坚强ぢ
伪装坚强ぢ 2020-12-23 20:08

For example, I get this compiler warning,

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

But I know that it\'s use

相关标签:
7条回答
  • 2020-12-23 20:59

    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
    
    0 讨论(0)
提交回复
热议问题