C # program structure

Source: Internet
Author: User

C # program structure
1. Write the first C # program and Note: 1. Write the program static void Main (string [] args) // write the code and output data in the Mian method {Console. writeLine ("Hello World! "); // Output" Hello World !" Console. ReadLine (); // print} 2. the C # program structure can be divided into comments, namespaces, classes, Main methods, identifiers, keywords, and statements. Note: All row Comments start with "//" and start with "/*". end with "*/". 2. namespace: C # The program uses namespace for organization, a namespace can be used either as an internal organizational system of a program or an organizational system that is made public to the outside (that is, a method that exposes its own program elements to other programs ). To call a class or method in a namespace, you must first use the using command to introduce the namespace. The using command imports the type members in the namespace into the current compilation unit. Using namespace name. Using N1; // use the using command to introduce the namespace N1namespace Test02 {class Program {static void Main (string [] args) {A oa = new (); // sample the Class A oa in N1. myls (); // call Myls method in class A }}namespace N1 // create namespace N1 {class A // declare A class in the namespace {public void Myls () {Console. writeLine ("Love you forever"); // output string Console. readLine () ;}}3. A class is a data structure that must be declared before any new class is used, once declared, a class 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 comply with the naming rules of the identifier. The class generally uses the first word. Namespace N1 // create namespace N1 {class A // declare A class in namespace {public void Myls () {Console. writeLine ("s"); // output string Console. readLine () ;}}3. The Main method has only one Main method in a C # program and must be static. Three Main methods can be modified: Public: indicates that the Main method is common, and the entire method can be called outside the entire class. Static: indicates that the method is Static, that is, This method belongs to the class, rather than the specific object of this class. Calling a static method does not allow the class to be materialized, but must be called directly using the class name. Void: This modifier indicates that the method has no return value. 4. Identifiers and keywords: The names of the identifiers have three basic rules: 1. the identifiers can only contain numbers, letters, and underscores; 2. The identifier must start with a letter or underline. 3. The identifier cannot be a keyword. 6. C # statement: the statement is a necessary unit for constructing all C # programs, you can declare local variables, constants, call methods, create objects, or assign values to variables, attributes, or fields. writeLine ("Hello World! "); 7. Naming rules: naming Rules play an important role in coding. 1. Use Pascal rules to name methods and types. Pascal naming rules indicate that the first letter must be capitalized, and the first letter of the connected word must be capitalized to define a public class, and create a public method public class DateGrid in this class // create a public class {public void DateBind () // create a public method {} in the public class. 2. Use the Camel rule to name the parameters of local variables and methods, this rule indicates that the first letter of the first word in the name declares a string variable in lowercase and creates a common string sturUserName; // declares a string variable sturUserNamepublic void addUser (string strUserName, byte [] byPassword); // create a public method with two parameters. 3. prefix "_" before all member variables "_". declare a private member Variable _ connectionStri in the public-class DateBase. Ng Public class DateBase // create a Public class {private string _ connectonString; // declare a private member variable} 4. The interface name is prefixed with "I" to create a public interface IconnectionString punlic interface Iconvertible // create a public interface Iconvertible {byte ToByte (); // declare a byte type method} 5. Name of the method, generally, name it a verb-object phrase. Create the CreateFlie method and the GetPath method public class Flie in the public File. // create a public class {public void CreatFlie (string fliePath) // create a CreatFlie method {} public void GetPath (string path) // create a GetPath method {}} 6. All member variables are declared at the top of the class, separate it from the method with a line feed and declare two private variables _ productId and _ productName public class Product at the top of the class // create a public class {private string _ productId; // declare the variable private string _ productName at the top of the class; // declare the variable private void AddProduct (string product, string productName) at the top of the class) // create a public method {} 7. Use a meaningful namespace, for example, the company name and product name use the company name and product name namespace Zivsoft // company name {} namespace ERP // product name {} 8. When using a control, try to name local variables. Create a method and declare the string variable title in the method so that it is equal to the Text 1 public string GetTitle () of the Label control. // create a public method 2 {3 string title = lbl_Title.Text; // define a local variable 4 rereturn title; // use this local variable 5}

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.