C # first knowledge,
C # program structure
I. Compile the first C # program and note:
1. Write a program
Static void Main (string [] args) // write the code output data in the Mian method {Console. WriteLine ("Hello World! "); // Output" Hello World !" Console. ReadLine (); // pause}
2. Notes
C # The program structure can be divided into comments, namespaces, classes, Main methods, identifiers, keywords, and statements.
Note: line comments start "//".
Segment comment starts with "/*" and ends "*/"
Ii. namespace:
C # A program uses a namespace for organization. A namespace can be used as an internal organizational system of a program, it can also be used as an organizational system that is exposed 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 ();}}}
Iii. Category
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.
[Class modifier] class [class name] [base class or interface]
{
[Class body]
}
A class name is a symbolic object. It must comply with the naming rules of the identifier. A class generally uses a noun with the first letter in upper case.
Namespace N1 // create namespace N1 {class A // declare A class in the namespace {public void Myls () {Console. writeLine ("s"); // output string Console. readLine ();}}}
Iii. Main Method
A c # program has only one Main method and must be static.
Three Main methods can be modified:
Public: The Main method is common. You can call the entire method outside the entire class;
Static: It indicates that the method is Static, that is, the method belongs to the class, rather than the specific object of the class. Calling static methods 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. identifier and keywords:
The name of the identifier has three basic rules:
1. The identifier can only contain numbers, letters, and underscores;
2. The identifier must start with a letter or underscore;
3. The identifier cannot be a keyword;
VI. C # statements:
A statement is a required unit for constructing all C # programs. It can declare local variables, constants, call methods, create objects, or assign values to variables, attributes, or fields.
Console.WriteLine("Hello World!");
VII. Naming rules:
Naming conventions 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.
Defines a public class and creates a public method in the class
Public class DateGrid // 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 means that the first letter of the first word in the name is in lowercase.
Declare a string variable and create a public Method
String sturUserName; // declare 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 _ connectionString in the public-class DateBase.
Public class DateBase // create a Public class {private string _ connectonString; // declare a private member variable}
4. The interface name is prefixed with "I"
Create a public interface IconnectionString
Public interface Iconvertible // create a public interface Iconvertible {byte ToByte (); // declare a byte type method}
5. Name a method. Generally, it is named as a verb-object phrase.
Create the CreateFlie and GetPath methods in the public File.
Public class Flie // 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 and separated from the method by a line break.
Declare two private variables _ productId and _ productName at the top of the class
Public class Product // create a public class {private string _ productId; // declare the variable private string _ productName at the top of the class; // declare the variable public void AddProduct (string productld, string productName) at the top of the class // create a public method {}}
7. Use a meaningful namespace, such as a company name or product name.
Use the company name and product name namespace
Namespace Zivsoft // company name {} namespace ERP // product name {}
8. Use a control to name local variables whenever possible.
Create a method to declare the string variable title in the method so that it is equal to the Text of the Label Control
1 public string GetTitle () // create a public method 2 {3 string title = lbl_Title.Text; // define a local variable 4 return title; // use this local variable 5}