如何在C#中注释/记录替代? [英] How to comment/document an override in C#?

查看:163
本文介绍了如何在C#中注释/记录替代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我会覆盖基类中的方法.有时我什至用空方法重写它们,因为我想要的是防止这种行为.

Sometimes I override methods in base classes. Sometimes I even override them with an empty method, because what I want is to prevent the behavior.

过去,我会写这样的东西来显示绕过基本方法的意图:

In the past I would write something like this to show the intent of bypassing the base method:

protected override void OnMouseUp(MouseEventArgs e)
{
    // base.OnMouseUp(e);
}

(我知道注释的代码行是一件坏事.我曾经用来做到这一点)

(I know a commented line of code is a bad thing. I used to do it)

但是我想做得更好:

  • 如何记录替代的意图?:
  • 我应该在覆盖的XML中写什么(<summary>?)文档?
  • How do I document the intention of the override? specifically:
  • What do I write in the override's XML (<summary>?) documentation?

推荐答案

类似

// This method is intentionally blank because 
// we do not want the base class functionality

// base.SomeMethod();

第一个注释清楚地说明了您为什么做您的工作,而随后出现的下一个开发人员将不必怀疑对base方法的调用是否被意外注释掉了.

The first comment clearly states why you did what you did, and the next developer who comes along won't have to wonder if the call to the base method was commented out accidentally.

如果您可以控制基类,则最好删除该方法并使类更抽象.然后,您可以选择仅在需要的子类中实现该功能.

If you have control over the base class, it may be better to remove that method and make the class more abstract. Then you can choose to only implement that functionality in child classes where it's needed.

这篇关于如何在C#中注释/记录替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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