Initial knowledge of C # program structure

Source: Internet
Author: User
Tags naming convention

Write the first C # program, Comment:  1, write program  static void Main (string[] args)//write code output data under Mian method         {            Console.WriteLine ("Hello world! ");//Output" Hello world! "            console.readline ();//print        }2, comment  c# program structure into comments, namespaces , classes, main methods, identifiers, and keywords, statements.   Note: line comments begin with the "//"    segment Comment Reference "/*", ending with "*/"   Two, namespaces: the  c# program uses namespaces for organization, and namespaces can be used as internal organizational systems for programs. It can also be used as an organization system that is exposed externally (that is, a way of exposing program elements that you own to other programs).   If you want to invoke a class or method in a namespace, you first need to introduce a namespace using a using directive that imports the type members in the namespace into the current compilation unit. The using namespace name.   using N1;    //using a using directive to introduce namespaces N1namespace test02{    class program    {        St atic void Main (string[] args)         {            A OA = new A ();//Sample N1 Classes in a            OA. Myls ();//Call the Myls method in Class A        }   }}namespace N1    //Create a namespace n1{  &nbsp Class A      //Declare a Class A in the namespace     {        public void Myls ()     &NBS P   {            Console.WriteLine ("Love You with Your Life");//output string           &N Bsp Console.ReadLine ();       }   }} Third, class   class is a data structure that must be declared before any new class is used. Once a class is declared, it can be used as a new type to use the  [class modifier] class[class name] [base class or interface] { [class body] }  class name is a representation body, it must conform to the naming rules of the identifiers, Classes generally use the first capitalized noun   namespace N1    //to create a namespace n1{    class A      // Declare a Class A     {        public void Myls ()         {      in a namespace       Console.WriteLine ("S");//output string             Console.ReadLine ();  & nbsp    }   }}  Three, the main method   There is only one main method in a C # program and must be static.   Three can modify the Main method:  public: It shows that the main method is common, and that the entire method can be called outside the entire class;  static: The description method is static, that is, the method belongs to the skill of the class, not theThe specific object of the class. Calling a static method does not make the class's manifested object, it must be called directly using the class name;  void: This modifier indicates that the method has no return value.   Four, the identifier and the keyword:  identifier naming has 3 basic rules:  1, identifiers can only be composed of numbers, letters and underscores;  2, identifiers must be preceded by letters or underscores;  3, identifiers cannot be keywords;  The C # statement:  statement constructs all C # program prerequisites and can declare local variables, constants, invoke methods, create objects, or assign values to variables, properties, or fields   console.writeline ("Hello world! "); The naming specification:  naming conventions play an important role in writing code.   1, using Pascal rules to name methods and types, Pascal's naming convention is that the first letter must be uppercase, and the first letter of the concatenated word is uppercase   defines a public class, and a public method is created in the class  public class Dategrid        //Create a public class {      public void Datebind ()        //in the common Class to create a public method        {       }}     2, using the Camel rule to name the parameters of local variables and methods, which refers to the first word in a name The first letter lowercase   declares a string variable and creates a public method  string sturusername;  //Declare a string variable sturusernamepublic void AddUser (String strusername, Byte[]bypassword);//Create a public method 3 with two parameters, All member variables prefixed with "_" .  declare a private member variable in the public class Datebase _connectionstring  public class datebase//Create a public class {    & nbsp private string _connectonstring;Declare a private member variable  } 4, prefix the name of the interface with "I"   Create a public interface Iconnectionstring punlic interface iconvertible// Create a common interface iconvertible{        byte tobyte ();//Declares a byte-type method} 5, the name of the method, which is generally named as a dynamic-Pentax phrase   creating a C in a public file The Reateflie method and GetPath method   public class flie//create a common class {        public void Creatflie (string Fliepath)//Create a Creatflie method         {       }        public voi d GetPath (string path)//Create a GetPath method         {       }}  6, all member variables are declared at the top of the class with a A newline separates it from the method   declares two private variables at the top of the class _productid and _productname  public class product//create a public class {        private string _productid;//Declare a variable at the top of the class         private string _productname;//Declare the variable at the top of the class   & nbsp     private void Addproduct (String product,string productName)//Create a public method         {  &NB Sp    }}  7, naming namespaces with meaningful names, such as companyName, product name   name namespace with company name and product name  namespace Zivsoft  //company Name {}namespace erp//product name {} 8, when using a control, try to name the local variable.   Create a method that declares the string variable title in the method, making it equal to the label control's text   1 public string GetTitle ()  //Create a common method 2 {3   &NB Sp     String title=lbl_title.text;//defines a local variable 4         Rereturn title;//Use this local variable 5} 

Knowledge of C # program structure

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.