什么是使用XML注释在C# [英] What is the use of XML comments in C#

查看:176
本文介绍了什么是使用XML注释在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是在C#中使用XML注释比信号线和多行注释。

  i.Single线
例如:
//这是一个单行注释

II。多行(/ * * /)
例如:
/ *这是一个多行注释
我们在2号线
注释* /
$的最后一行b $ⅢB族。 XML评论(///)。
例如:
///摘要;
///多语种语言设置错误消息。
///总结


解决方案

从<一个HREF =http://msdn.microsoft.com/en-us/library/b2s063f7.aspx相对=nofollow> XML文档注释(C#编程指南):




当您使用/文件选项编译,编译器将搜索
在源代码中所有的XML标签,并创建一个XML文档文件。




也为智能感知使用Visual Studio的XML注释:

  ///<总结> 
///这个类执行的重要功能。
///< /总结>
公共类MyClass的{}



会给你,当你输入代码或悬停漂亮的提示光标移到部件,其具有XML注释:





注意:通常你应该添加XML注释仅公开可见的类型或成员。如果成员是内部的还是私有的,那么它是好的,但不是必需的。有很好的工具 GhostDoc (可扩展到Visual Studio),它可以从类型生成XML的意见或成员名字。这是很好的检查,如果你有良好的命名 - 如果生成的评论是不明确的,那么你应该提高成员的名字



我也建议使用简单的(非XML)评论少,越好。因为评论是重复代码的形式 - 它重复,你已经在你的代码信息。这里是两个问题:




  • 您的代码不够清晰,你应该改进它(重命名,提取类或成员),而不是添加注释

  • 在当前代码修改,意见往往保持不变(程序员是懒惰的)。因此,当时间流逝的意见变得过时和混乱。



好意见应当说明的为什么的你写代码,而不是复制的什么的代码在做什么。


What is the use of XML comments in C# than signal line and multiple line comments.

i.Single line
Eg:
//This is a Single line comment

ii. Multiple line (/* */)
Eg:
 /*This is a multiple line comment
We are in line 2
Last line of comment*/

iii. XML Comments (///).
Eg:
/// summary;
///  Set error message for multilingual language.
/// summary

解决方案

From XML Documentation Comments (C# Programming Guide):

When you compile with the /doc option, the compiler will search for all XML tags in the source code and create an XML documentation file.

Also XML comments used by Visual Studio for IntelliSense:

/// <summary>
///  This class performs an important function.
/// </summary>
public class MyClass{}

Will give you nice hints when you are typing code or hovering cursor over member which has xml comments:

NOTE: Usually you should add xml comments only to publicly visible types or members. If member is internal or private, then it's good, but not necessary. There is nice tool GhostDoc (available as extension to Visual Studio) which can generate XML comments from type or member name. It's nice to check if you have good naming - if generated comment is not clear, then you should improve name of member.

I also suggest use simple (non-xml) comments as little, as possible. Because comment is a form of code duplication - it duplicates information which you already have in your code. And here is two problems:

  • Your code is not clear enough and you should improve it (renaming, extracting classes or members) instead of adding comments
  • When code changes, comments often stay unchanged (programmers are lazy). So when time passes comments become obsolete and confusing.

Good comments should describe why you writing code instead of duplicating what code is doing.

这篇关于什么是使用XML注释在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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