Familiar with Java with C or C , it will find that C #'s grammar is very familiar, it is easy to get started. It is mainly due to Java mainly: (only to list only one of them)
1, case difference 2, the Boolean type Bool in C #, while Java is Boolean3, C # has an insecure mode, can be used in this mode, the pointer 4, the enumeration type 5, the agent, and the indexer 6, Operator overload 7, no throws keyword
Let's start the C # trip.
Before you start learning C #, you understand the characteristics of the C # statement:
1. C # is different size. 2. Each sentence is ended with a semicolon, not like VB.NET. 3. Multiple statements can be written in one line through the semicolon, but it is not recommended because it is not clear. OK, start.
First understand the type of data:
Bool
True or False
Byte
8-bit unsigned integer
Short
16-bit integer
int
32-bit integer
Long
64-bit integer
Float
32-bit floating point
Double
64-bit floating point number
charr
16 characters
String
Multiple 16-bit characters
The width is narrow (i.e. the number of bytes) data type can directly assign a wide data type and automatically convert to a new type. example:
Float
a
=
9.9f
;
int
b
=
4
a
=
B;
//
The above converts a integer 4 into floating point.
It is also possible to convert a wide-class narrow type example of a wide type by mandatory type conversion:
Float
a
=
9.9f
;
int
b
=
4
; B
=
(
int
) a;
//
Note that the name of the type to be converted must be placed in parentheses and placed in front of it.
String and numerical interchange:
String
a
=
Convert.toString (b);
Float
a
=
Convert.TOSINGLE (B); can also format the output as a C language:
Float
a
=
3.1415926728F
;
String
B
=
a.tostring
"
##. ##
"
);
Variables and constant statements:
C # Allows a statement to declare multiple variables INT A, B. However, multiple statements are written into multiple statements for the statement clearly. One thing to note is that if there is no fraction in declating numeric variables, it is automatically integer type. If there is a decimal part, it is automatically dunk type, not a float type, which can be converted to different types:
Float a = 1.2f; long b = 23L;
For constant statements, habits use uppercase symbols to represent constants, such as:
CONST FLOAT PI = 3.1415926;
For character declarations, you have enclosed in single quotes, such as:
CHAR A = 'D';
It can also be assigned:
INT A = b = c = 2;
Although it is convenient, it is not recommended, it is recommended to write it into three statements, so that the structure is also clear.
(Note: The length of the variable name of the C # is arbitrary, which can be mixed by numbers and case letters, but the first must be letters.
About the escape character in C #:
/ N --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----- Enter / T ------------------------- Table / b ----------- -------------- Escape / f ------------------------ Change page / 0 --- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - Double quotation number / '------------------------- Single number // ---------------- --------- The backslash also has a more special, @ 号, as long as the statement is added to @ 号, ie, the @ 号 有 有 有 转 按 按 按 按 按 运 运 运 运 运 运 运 运 运 运 运 运 运 运 运 运
-------------------------------------------------- ----------------
Still first look at an example, because you have experience, look at the statement, very bored ^ _ ^
Using
System;
Class
Sample
{Static void main (string [] args) {string hey = "I hope I will wish"; console.writeline ("C # Quick Start," hey);}}
A very headed analysis: USING statement shows that the code library to use in the program is a class name, which may include multiple classes, all things in the class must be placed in braces, and the entire program begins with the main function. Press F5 to compile execution. Ok, complete the first C # program. Let's take a bothering syntax:
Operator:
Arithmetic operator: --------------------- - ------------------- * ------------------------------- In addition to% --- ------------------ Remove (ie the remainder after the removal) logic operators: & ------------------ --- Bit and | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --- Press or ~ -------------------- Reverses >> N ------------------------------------------------------------------------------------------------ --- Right movement N-bit << n ----------------- left shift N-bit increment Decrease Operators:
or = or - = or * = or / =
Such as i = 1; i ; ie = i 1; other as the same reason, do not say.
Comparison operators:> or
C # conditions judge:
IF (a> 0) {a = -1;} else {console.writeline (a);} The condition must be placed in parentheses.
The judgment of two conditions, pay attention to the equal number IF ((a == 0) && (B> 0)) {a = -1; b = -1;} else = -1; b = -1;} else = -1; b = -1;} else = -1; B }
Switch is used for multiple possible values. If there is a match, perform the corresponding code, each sentence must have a BREAK statement interrupt the execution of the following statement. Switch (a) {case 1: console.writeline ("1"); break; case 2: console.writeline ("2"); break; case 3: console.writeline ("3"); Break; Default: Console .Writeline ("default");} While loop: -------------------------------------- ----- A = 1; While (a <20) {a = = a;} As long as the condition is true, it is cycled to A until a <20 --------- --------------------------------- Do-While cycle: ------------ ---------------------------- A = 1; do {a = = a;} while (a <20); ------------------------------------------ DO while loop is similar to the While loop, The difference is that the Do-While loop is executed at least one A = A;
For loop: ----------------------------------------- INT J = 1 ; for (int i = 0; i <100; i ) {j ; console.writeline ("i =" i "j =" j);
} OK, the annoying basic syntax is completed.