C # Coding Standard (i)

Source: Internet
Author: User
Tags naming convention

Always want to write a code of their own standards, after a period of excellent open source code observation and look at other people write standards, feel good code to people feel is comfortable, also very important. So put them in summary, for later forget, in addition, usually write code when you can refer to. The following example is primarily based on Microsoft's code.

  Naming conventions pascalcasingpascalcasing each word with the first letter capitalized and the remaining letters lowercase. For example: Fileaccess,arraysegment and so on.   In addition to parameters, variables, constants, names of all namespace names, classes, functions, interfaces, properties, events, enumerations, and so on, use the Pascal style.  camelcasing camelcasing the first letter in lowercase and the first letter of the remaining words capitalized. For example: Propertyname,filepath and so on.   parameter and variable naming use Camelcasing. screaming_caps screaming_caps all letters in each word are capitalized, the word is connected with the word "_", and the style is currently in C # Used only in const constants.   such as: Public const string default_page = "default.aspx";  private variable named  private private variables Use underscore "_" or "m_" + Camelcasing the casing rules to quickly confirm the scope of the variable.   such as: private int _userid;  private int m_userid;  One, naming convention and style  1, type name and method name with Pascal naming specification:       public class stringreader:textreader    {  public override String ReadToEnd ()   &N Bsp        {          ...           }} 2 , local variables and method parameters using the Camel naming convention:   internal static string Internalcopy (String sourceFileName, String destfilename, BOOL Checkhost)   {    string fullsourcefilename = path.getfullpathinternal (sourceFileName); ...   }  3, interface name plus prefix i:   public interface IComparer {         //compares two objects. An implementation of this method must return a         //value less than zero if X was less than Y, zero if x is equal to Y, or a         //value greater than zero if x is greater than y.         int Compare (object x, Object y);   } 4, private member variable preceded by m_ or _:   unsafe int Ernal class pathhelper  {       //maximum size, max be greater than Max path if Contai NS Escape sequence           private int m_capacity;          &NBSP ;//Current length (next character position)            private int m_length;    &N Bsp      //Max Path, May is less than Capacity           private int _maxpath; ....  }  5, constants using Pascal or All caps denote:     Private Const int format_message_ignore_inserts = 0x00000200;    Private Const int Format_message_from_system    = 0x00001000;    Private Const int Format_messa  Ge_argument_array = 0x00002000;     Const string RSAKeyValue  = Xmlsignatureconstantsnamespace + "RSAKeyValue";  6, The custom attribute class is appended with the suffix attribute:  [AttributeUsage (Attributetargets.struct | AttributeTargets.Class | attributetargets.assembly, AllowMultiple = True)]    [ComVisible (true)]    public sealed class Debuggervisualizerattribute:attribute 7, custom exception class plus suffix exception:  [serializable]    public class Argumentexception:systemexception, Iserializable 8, method naming uses "verb/object" pairs, such as GetHashCode (...),   public unsafe static Array CreateInstance (Type elementtype, int length)   9, the method name with the return value needs to describe this return value, such as getobjectstate ().&NBSP;10, use descriptive variable names.   A. Avoid using a single letter as a variable name, such as I or T, with index or temp instead.   B. Avoid the use of Hungarian notation for public or protected members.        C. Avoid using abbreviations (such as num for number).  11, do not confuse the use of predefined types in C # with types in the CLR:         such as Object and object, string and String,int with Int32 12, For generics, use the first letter of the type instead, when used. NET type is used only when the type suffix:  public class dictionary<k,v>  {...} &NBSP;13, use an opinion namespace name, such as product name or company name. &NBSP;14, avoid using fully qualified type names. Replace with a using statement.  15, put all the framework namespaces together on top, put customizations at the bottom, third-party namespaces in the middle:  using system;using system.collections;using System.collections.generic;using threadpartylibrary;using mycompanyname;16, using delegate inference, do not display instantiation of the delegate:  delegate Void Somedelegate ();p ublic void SomeMethod () {...} Somedelegate somedelegate = somemethod;17, use tab to indent. &NBSP;18, all member variables must be declared at the top, separated by one line from the property or method.  19, when defining a local variable, try to keep it close to the place where it was used for the first time. &NBSP;20, the file name can reflect the class it uses.  21, using the left curly brace ({), wraps a line.  22, if even if there is only one line of code, also add {} surrounded:  23, for each namespace to build a directory. such as MyProject.TestSuite.TestTier using Myproject/testsuite/testtier as the path.  24, when the code in a row is too long, it is recommendedBreak the break using the following method:  break after a comma----comma after the after a operator----operator prefer higher-level breaks to lower-level BR  Eaks----Align The new line with the beginning of the expression at the same level on the previous line.   such as Longmethodcall (EXPR1, expr2,         EXPR3, EXPR4, EXPR5);   examples o F breaking an arithmetic Expression:  prefer:   var = a * b/(c-g + f) +     &N Bsp    4 * z;   var = a * b/(c-g + F) +     4 * Z;  bad Style–avoid:&nbs P; var = A * b/(c-g +           f) + 4 * z; 25, initialize when declaring variables.  26, the Framework type inherited from stream ends with stream:  such as FileStream, MemoryStream, and so on.  27, when you do not understand a method, you can break them down into smaller parts and give them the proper names.

C # encoding standard (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.