继上一篇笔记之后, 今天我们讨论一下 代码中是存在注释是否是一件好的事情.
在我们开发的过程中讲究 "名副其实, 见名识意", 这也往往是很多公司的要求, 但是有了这些要求是不是我们的代码中如果存在注释是不是意味着我们的 函数, 变量, 以及类 的命名就不符合了 "名副其实, 见名识意".
我们先区分一下注释的类别, 注释一般分为以下几种:
1, 单行注释
2, 多行注释
3, 文档注释
4, #region 折叠注释, 可以将 代码折叠
注释的类别
1, 单行注释:
在以 "//" 开头, 用以说明一行代码的作用放置位置 看习惯或者公司要求合理就行. 常用于函数内部, 在很多的开源代码中文件的头部我同样见到很多人使用单行注释进行说明, 灵活就好.
体现形式如下:
- public List<string> getVipUserNameByUserType()
- {
- // Vip user name list
- var vipUserNames = new List<string>();
- foreach (var user in Users)
- {
- if (user.Type = "VIP")
- vipUserNames.Add(user.Name);
- }
- return vipUserNames;
- }
- View Code
2, 多行注释:
- /*
- * 作者:〈版权〉
- * 描述:〈描述〉
- * 创建时间: YYYY-MM-DD
- * 作用:
- */
- View Code
- /// <summary>
- /// MyClass
- /// </summary>
- public class MyClass
- {
- /// <summary>
- /// MyProperty
- /// </summary>
- public int MyProperty { get; set; }
- /// <summary>
- /// MyMethod
- /// </summary>
- public void MyMethod(){ }
- }
- View Code
- //Check to see if the employee is eligible for full benefits
- (1)If((employee.flags & HOURLY_FLAG)&& (employee.age>65))
- (2)If(employee.isEligibleForFullBenefits()))
- // Copyright (c) .NET Foundation. All rights reserved
- // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
- View Code
- public void ConfigureServices(IServiceCollection services)
- {
- // These two middleware are registered via an IStartupFilter in UseIISIntegration but you can configure them here.
- services.Configure<IISOptions>(options =>
- {
- });
- }
- View Code
来源: https://www.cnblogs.com/szlblog/p/11938872.html