从业多年,不断发现有很多团队的代码写的很随意,各种风格、命名方式都有,有用c++风格的,有用java风格的。所以本人在这里介绍下微软官方的C#编码规范,规范本身内容其实不多,但是如果能认证看一遍的话,相信对你以后的开发会有很大帮助。
注:本文不是一定适用所有人和所有团队,供大家参考。
如有需要,请大家制定出自己的代码规范之前,参考以下两个准则:
C#的命名有两种约定:Pascal和驼峰。
Student
,StudentName
,StudentParentName
。Student
,studentName
,studentParentName
.class
、record
及record
的参数、struct
的名称,如:public class StudentInfo
{
}
//record及record的参数
public record StudentAddress(string NameInfo,int Age);
public struct StudentDetail
{
}
I
,可以清楚的表明是interface
),如:public interface IStudentName
{
}
public
的成员也应为Pascal命名,这些成员包括字段、属性、事件。方法名也应遵循Pascal命名,无论其是否是public
。如:public class StudentInfo
{
//公共字段
public bool IsValid;
//公共属性
public IWorkQueue WorkQueue{get;set;}
//公共事件
public event Action EventProcessing;
//公共方法
public void Run()
{
}
}
private
或internal
字段时使用驼峰命名,且字段名应以_
开头。如:public class DataService
{
private IWorkerQueue _workerQueue;
}
static
的private
或internal
的字段,则字段名应该以s_
开头,对于线程静态则应该使用t_
开头。如:public class DataService
{
private static IWorkerQueue s_workerQueue;
[ThreadStatic]
private static TimeSpan t_timeSpan;
}
public T SomeMethod<T>(int someNumber, bool isValid)
{
}
//正确
int a=0;
int b=0;
//错误
int a=0;int b=0;
public int Age{get;set;}
//正确,此注释行的上方留了一行空白,不能贴紧
public void Run()
{
}
public void Run()
{
}
以下是错误的:
public void Run(){
}
int i=0;//定义i并初始化
以下是正确的做法:
// 定义i并初始化. Define i and init.
int i=0;
//定义i并初始化
int i=0;
public
的成员都要有注释。尽量使用自带的Func<>
和Action<>
,避免自定义委托。
使用using
来简化代码,简化资源的Dispose
:
Font font1 = new Font("Arial", 10.0f);
try
{
byte charset = font1.GdiCharSet;
}
finally
{
if (font1 != null)
{
((IDisposable)font1).Dispose();
}
}
以上代码可以简化为:
using (Font font2 = new Font("Arial", 10.0f))
{
byte charset2 = font2.GdiCharSet;
}
在C# 8中可以进一步简化为:
using Font font3 = new Font("Arial", 10.0f);
byte charset3 = font3.GdiCharSet;
以下是正确的:
var instance3 = new ExampleClass { Name = "Desktop", ID = 37414,
Location = "Redmond", Age = 2.3 };
以下是不建议的:
var instance4 = new ExampleClass();
instance4.Name = "Desktop";
instance4.ID = 37414;
instance4.Location = "Redmond";
instance4.Age = 2.3;
如果你在定义一个稍后不需要手动删除的事件处理程序,则应该使用lambda表达式:
public Form2()
{
this.Click += (s, e) =>
{
MessageBox.Show(((MouseEventArgs)e).Location.ToString());
};
}
以下是错误的,从名字上看inputInt
应该是int
类型,但其实是string
类型。
var inputInt = Console.ReadLine();
Console.WriteLine(inputInt);
以下都是正确的:
for (var i = 0; i < 10000; i++)
{
manyPhrases.Append(phrase);
}
foreach (char ch in laugh)
{
if (ch == 'h')
Console.Write("H");
else
Console.Write(ch);
}
因为laugh
集合中的元素类型是什么并不明显,使用var
的话可读性不强,不如直接明确char
。
以下是符合C# 编码规范的标准范文,你可以进行参考:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using Microsoft.Win32;
namespace System.Collections.Generic
{
public partial class ObservableLinkedList<T> : INotifyCollectionChanged, INotifyPropertyChanged
{
private ObservableLinkedListNode<T> _head;
private int _count;
public ObservableLinkedList(IEnumerable<T> items)
{
if (items == null)
throw new ArgumentNullException(nameof(items));
foreach (T item in items)
{
AddLast(item);
}
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
public int Count
{
get { return _count; }
}
public ObservableLinkedListNode AddLast(T value)
{
var newNode = new LinkedListNode<T>(this, value);
InsertNodeBefore(_head, node);
}
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
NotifyCollectionChangedEventHandler handler = CollectionChanged;
if (handler != null)
{
handler(this, e);
}
}
private void InsertNodeBefore(LinkedListNode<T> node, LinkedListNode<T> newNode)
{
...
}
...
}
}
引用: