我可以安全地从网页事件处理程序中删除未使用的参数吗,为什么Page_Load中的参数未被使用? [英] Can I safely remove unused parameters from Web page event handlers and why are parameters in Page_Load not unused?

查看:69
本文介绍了我可以安全地从网页事件处理程序中删除未使用的参数吗,为什么Page_Load中的参数未被使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了成为一个精巧的程序员,我尝试修复构建警告.但是我陷入了页面事件处理程序中未使用参数的警告.示例:

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
    }

如果我遵循Visual Studio的建议并删除未使用的参数 sender e ,我将更改方法签名,并且代码可能无法正常工作.但是,根据此microsoft帮助页,事件处理程序的签名确实可以不看参数.该页面的语言让我感到困惑,例如 breaking 之类的术语,但是在这种情况下,我认为删除未使用的参数确实是安全的.

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.

但是下一个事件处理程序怎么样?在该事件处理程序中,也不使用参数,但是VisualStudio不会发出相同的警告:

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;
}

我的问题是:

  • 这些参数真的没有使用吗?
  • 帮助页面中的 breaking 一词是什么意思?
  • Page_PreInit和Page_Load的事件处理程序类型之间有什么区别?
  • 如帮助页面所述,未使用的参数真的会招致维护和性能成本吗?
  • 抑止未使用的参数"警告也许是最佳实践?

推荐答案

要依次回答我自己的问题,

To answer my own questions, in order:

  • 以后可能需要这些参数,因此并不是真正不需要的"(请参阅​​Michael Gorsich的评论)
  • Microsoft术语诸如 breaking 之类的含义无所谓,只要他们谈论诸如不需要"之类的可辩论之类的东西即可;我会建议使用新的行话,例如不需要的构建警告
  • 两个事件处理程序都相似,因为它们可以接收相同数量和类型的参数,这是VisualStudio(或DevExpress CodeRush ??)的奇怪行为.
  • 未使用的参数不会产生维护和性能成本,相反,删除这些参数可能会在以后需要它们时产生成本,并且开发人员必须知道"可以重新添加它们.
  • 我现在正在寻找如何配置VS来抑制所有有关未使用参数的警告
  • 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

这篇关于我可以安全地从网页事件处理程序中删除未使用的参数吗,为什么Page_Load中的参数未被使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆