Can I safely remove unused parameters from Web page event handlers and why are parameters in Page_Load not unused?

与世无争的帅哥 提交于 2020-02-05 04:21:10

问题


In order to be a neat programmer I try to fix the build warnings. But I get stuck with the warnings for unused parameters in page event handlers. Example:

public partial class MyPage : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        some code not referencing sender or e
    }

If I would follow upon the recommendation of VisualStudio and remove the unused parameters sender and e, I would alter the method signature and the code might not work correctly. However, according to this microsoft help page the signature for event handlers does not look at the parameters. The language of that page is confusing to me, with jargon like breaking, but in this case I guess that it is indeed safe to remove the unused parameters.

But how about the next event handler, where the arguments are also not used, but VisualStudio does NOT issue the same warning:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack && !IsCallback)
        InitPagina();
    return;
}

My questions are:

  • Are these parameters really unused and unneeded?
  • What is meant with the term breaking in the help page?
  • What is different between the event handler types for Page_PreInit and Page_Load?
  • Do unused parameters really incur maintenance and performance costs, as the help page states?
  • Should it perhaps be best practice to suppress the Unused Parameters warnings?

回答1:


I'm not 100% sure. But i think that if you remove the parameters the function may not get called anymore. Since the expected signature changed.




回答2:


To answer my own questions, in order:

  • These parameters may be needed later, so are not really "unneeded" (see comment by Michael Gorsich)
  • It does not matter what the meaning is of Microsoft jargon like breaking, if they talk about debatable things like "unneeded"; I'd propose new jargon like unneeded build warnings
  • Both event handlers are similar in that they can receive the same number and type of arguments, it is just strange behaviour by VisualStudio (or DevExpress CodeRush??)
  • Unused parameters do not incur maintenance and performance costs, instead, removing those may incur costs later when they may be needed and the developer has to "know" that they can be added back in
  • I am now looking how to configure VS to suppress all warnings about unused parameters


来源:https://stackoverflow.com/questions/41162335/can-i-safely-remove-unused-parameters-from-web-page-event-handlers-and-why-are-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!