C# 代碼標準 .NET2.0版(一)命名和風格

來源:互聯網
上載者:User

 

1.Use Pascal casing for type and method names and constants:

public class SomeClass
{
   const int DefaultSize = 100;
   public SomeMethod( )
   {}
}

 

2.Use camel casing for local variable names and method arguments:

int number;
void MyMethod(int someNumber)
{}

 

3.Prefix interface names with I:

interface IMyInterface
{..}

 

4.Prefix private member variables with m_.

5.Suffix custom attribute classes with Attribute.

6.Suffix custom exception classes with Exception.

7.Name methods using verb/object pairs, such as ShowDialog( ).

8.Methods with return values should have names describing the values returned, such as GetObjectState( ).

9.Use descriptive variable names.

10.Avoid single-character variable names, such as i or t. Use index or temp instead.

11.Avoid using Hungarian notation for public or protected members.

12.Avoid abbreviating words (such as num instead of number).

13.Always use C# predefined types, rather than the aliases in the System namespace. For example:

object NOT Object
string NOT String
intNOT Int32

 

14.With generics, use capital letters for types. Reserve suffixing Type for when dealing with the .NET type Type:

//Correct:
public class LinkedList<K,T>
{...}
//Avoid:
public class LinkedList<KeyType,DataType>
{...}

 

15.Use meaningful namespace names, such as the product name or the company name.

Avoid fully qualified type names. Use the using statement instead.

Avoid putting a using statement inside a namespace.

Group all framework namespaces together and put custom or third-party namespaces underneath:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using MyCompany;
using MyControls;

 

16.Use delegate inference instead of explicit delegate instantiation:

delegate void SomeDelegate( );
public void SomeMethod( )
{...}
SomeDelegate someDelegate = SomeMethod;

 

17.Maintain strict indentation. Do not use tabs or nonstandard indentation, such as one space. Recommended values are three or four spaces.

18.Indent comments at the same level of indentation as the code that you are documenting.

19.All comments should pass spellchecking. Misspelled comments indicate sloppy development.

20.All member variables should be declared at the top, with one line separating them from the properties or methods:

public class MyClass
{
   int m_Number;
   string m_Name;

   public void SomeMethod1( )
   {}
   public void SomeMethod2( )
   {}
}

 

21.Declare a local variable as close as possible to its first use.

22.A filename should reflect the class it contains.

23.When using partial types and allocating a part per file, name each file after the logical part that part plays. For example:

//In MyClass.cs
public partial class MyClass
{...}
//In MyClass.Designer.cs
public partial class MyClass
{...}

 

24.Always place an open curly brace ({) in a new line.

25.With anonymous methods, mimic the code layout of a regular method, aligned with the anonymous delegate declaration (this complies with placing an open curly brace in a new line):

delegate void SomeDelegate(string someString);
//Correct:
public void InvokeMethod( )
{
   SomeDelegate someDelegate = delegate(string name)
                               {
                                  MessageBox.Show(name);
                               };
   someDelegate("Juval");
}
//Avoid
public void InvokeMethod( )
{
   SomeDelegate someDelegate = delegate(string name){MessageBox.Show(name);};
   someDelegate("Juval");
}

 

26.Use empty parentheses on parameter-less anonymous methods. Omit the parentheses only if the anonymous method could have been used on any delegate:

delegate void SomeDelegate( );
//Correct
SomeDelegate someDelegate1 = delegate( )
                             {
                               MessageBox.Show("Hello");
                             };
//Avoid
SomeDelegate someDelegate1 = delegate
                             {
                                MessageBox.Show("Hello");
                             };

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.