C # programming naming rules,

Source: Internet
Author: User

C # programming naming rules,

Wang Xiaoming, his elders must have a surname Wang. Isn't there a family named Wang in the village of penghe village? Maybe it's the child of the family. A lost child will successfully go home.

The naming convention is like naming a person. It conveys some information from the name, such as the scope and type, which can play a well-known role. during development, A good naming convention can improve the development efficiency. In particular, during team development, each person has different naming rules. During calling or maintenance, it takes time to figure out the author's intention to create a class or field.

  • Namespace: use the format <Company>. <Compinent>, for example, Weimei. study, this specification is mainly used to prevent duplicate namespaces when a third-party library is referenced, resulting in obfuscation of codes;
  • Class, interface, method, and attribute: Use Pascal naming rules, such as GetDescript;
  • Variable: Use the Camel naming rules, for example, personCount;
  • Constants: all uppercase words are separated by underscores (_), for example, GROW_UP_AGE.
    namespace Weimei.Study{  class Person     {        public const int GROW_UP_AGE = 18;        private string _name;        public string Name;        {              get{ return _name;}             set{ _name = value;}        }        public int Age { get; set; }    }    interface IPersonService    {        string GetDescript();    }}

     

 

Use a prefix or suffix as the identifier to differentiate different functions or types:

  • Interface: Use the letter "I" as the prefix;
  • Static variables: Use "s _" as the prefix;
  • Object variable: Use "m _" as the prefix;
  • Member variable: Use "_" as the prefix.
    Namespace Weimei. study {class Program {static void Main (string [] args) {Person m_person = new Person (); m_person.Name = "Wang Xiaoming"; m_person.Age = 12; IPersonService m_ps = new PersonService (m_person); string result = m_ps.GetDescript (); Console. writeLine (result); Console. readLine () ;}} class Person {public const int GROW_UP_AGE = 18; // adult age private string _ name; public string Name {get {return _ name ;} set {_ name = value ;}} public int Age {get; set ;}} interface IPersonService {string GetDescript ();}
    Class PersonService: IPersonService {Person _ m_person; public PersonService (Person m_person) {_ m_person = m_person;} public string GetDescript () {string desc = "minor "; if (_ m_person.Age> = Person. GROW_UP_AGE) {desc = "adult";} return _ m_person.Name + desc ;}}}

    Note:

  • When naming a variable, do not start it at will, such as the int a variable. Others should know what the variable is for and check the following code. If the variable is used to count, it should be named xxCount;
  • Do not use the same name as the built-in class to avoid confusion;
  • Using the prefix and suffix is counterproductive.

 

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.