C# 编码规范1

本文档详细介绍了C#编程语言中推荐使用的命名规范和代码风格,包括类型、方法和变量的命名规则,以及一些代码布局和组织的最佳实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Naming Conventions and Styles

  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.

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

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

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

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

    object NOT Object
    string NOT String
    intNOT Int32

  11. 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>
    {...}

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

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

  14. Avoid putting a using statement inside a namespace.

  15. 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");
    };
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值