本文档描述了使用C#作为语言在.NET中开发软件应用程序和类库的规则和建议。主要目的是定义通用的指导方针,以实施一致的编码风格和格式,这对开发人员避免在使用c#开发软件应用程序时经常犯的错误是有用的。本文档涵盖了命名约定、编码风格和一些体系结构级别的建议。

编码标准的目的和实现它的最佳实践

编码标准是一套用于编程语言的指导方针,它推荐编程风格和最佳实践来实现它。

编码标准通常包括缩进、注释、命名约定、编程实践、项目中的文件结构、架构最佳实践等。强烈建议软件开发人员遵循这些指导方针。编码指南有以下优点。

命名约定

在C#编程中有三种常用的命名约定,

例如:HelloWorld

例如:helloWorld

例如:string m_sName; string strName; int iAge;

c语言安全编码指南(编码指南和最佳实践)(1)

控制前缀命名约定

在开发WEB和窗口应用程序时,通常使用的最佳实践是为UI元素使用前缀。因为ASP.NET和Windows Form有太多可用的控件,因此可以用表格来总结它们。

c语言安全编码指南(编码指南和最佳实践)(2)

代码缩进和注释

  1. 私有成员
  2. 私有财产
  3. 公共属性
  4. 构造函数
  5. 事件处理程序/操作方法
  6. 私有方法
  7. 公共方法

良好的编程实践

Good:privatevoidSaveAddress(Addressaddress){} Bad: //Thismethodusedtosaveaddress privatevoidSave(Addressaddress){}

Good:publicvoidUpdateAddress(Addressaddress){} publicvoidInsertAddress(Addressaddress){} Bad:publicvoidSaveAddress(Addressaddress){ if(address.AddressId==0){}else{} }

Good:publicclassEmployeeController:Controller{ publicActionResultIndex(){} publicActionResultCreate(){} [HttpPost] publicActionResultCreate(EmployeeModelemployee){} publicActionResultEdit(intid){} [HttpPut] publicActionResultUpdate(EmployeeModelemployee){} [HttpDelete] publicJsonResultDelete(intid){} } Bad:publicclassEmployeeController:Controller{ publicActionResultGetAll(){} publicActionResultCreateEmployee(){} [HttpPost] publicActionResultCreateEmployee(EmployeeModelemployee){} publicActionResultEditEmployee(intid){} [HttpPut] publicActionResultUpdateEmployee(EmployeeModelemployee){} [HttpDelete] publicJsonResultEmployeeDelete(intid){} }

在上面的例子中,你谈论的是employee,那么不应该有带有employee前缀或扩展名的动作方法名

Good: intage; stringfirstName; objectaddressInfo; Bad: System.Int32age;StringfirstName; ObjectaddressInfo;

Good:if(firstName.ToLower()=="yogesh"){} if(firstName.ToUpper()==“YOGESH”){} Bad:if(firstName==“rohit”){}

Good:if(firstName==String.Empty){} Bad:if(firstName==“”){}

Good:publicenumLoggerType{ Event, File, Database } publicvoidLogException(stringmessage,LoggerTypeloggerType){ switch(loggerType){ caseLoggerType.Event: //Dosomethingbreak; caseLoggerType.File: //Dosomethingbreak; caseLoggerType.Database: //Dosomethingbreak; default: //Dosomethingbreak; } } Bad:publicvoidLogException(stringmessage,LoggerTypeloggerType){ switch(loggerType){ case"Event": //Dosomethingbreak; case"File": //Dosomethingbreak; case"Database": //Dosomethingbreak; default: //Dosomethingbreak; } }

Good:publicContactGetContactDetails(Addressaddress){ if(address!=null&&address.Contact!=null){ returnaddress.Contact; } } Bad:publicContactGetContactDetails(Addressaddress){ returnaddress.Contact; }

Good: “Erroroccurredwhileconnectingtodatabase.Pleasecontactadministrator.”“Yoursessionhasbeenexpired.Pleaseloginagain.” Bad: “ErrorinApplication.” “Thereisanerrorinapplication.”

Good:publicvoidUpdateAddress(Addressaddress){} Bad:publicvoidUpdateAddress(intaddressId,stringcountry,stringstate,stringphoneNumber,stringpinCode,stringaddress1,stringaddress2){}

  1. 返回集合时,如果没有要返回的数据,则返回空集合,而不是返回null。
  2. 总是检查Any()运算符,而不是检查count,例如:collection.Count > 0和空值检查
  3. 遍历时使用foreach而不是for循环。
  4. 使用IList<T>、IEnumerable<T>、ICollection<T>而不是具体的类,例如使用List<>

Good:varemployee=newEmployee{ FirstName=“ABC”,LastName=“PQR”,Manager=“XYZ”,Salary=12346.25 }; Bad:varemployee=newEmployee(); employee.FirstName=“ABC”; employee.LastName=“PQR”; employee.Manager=“XYZ”; employee.Salary=12346.25;

usingSystem; usingSystem.Collections.Generic;usingSystem.IO; usingSystem.Text; usingCompany.Product.BusinessLayer;

Good:using(varfileToOpen=newFileInfo(fileName)){ //Fileoperation } Bad:varfileInfo=newFileInfo(fileName); try{ //Fileoperation }finally{ if(fileInfo!=null){ fileInfo.Delete(); } }

voidReadFile(stringfileName){ try{ //readfromfile. }catch(System.IO.IOExceptionfileException){ //logtheerror.Re-throwexceptionthrowfileException; }finally{} } Bad:voidReadFile(stringfileName){ try{ //readfromfile. }catch(Exceptionex){ //catchinggeneralexception }finally{} }

架构设计级别指南

publicclassMyClass<T>whereT:SomeOtherClass{ publicvoidSomeMethod(Tt){ SomeOtherClassobj=t; } }

,