It has been a long time to use C # programming. I think about some of the fragments and paths I learned in the past, and sometimes it's just a waste of time. To be honest, there is no detour, and it is still the 18 th bend of the road. It is too great to learn a technology. Every teaching material that you contact is always more or less wrong, this is quite fatal for a learner, because it is likely that he or she has taken the wrong path due to wrong guidance.
The building starts on a ground in the high-rise area.
Learning any language starts with the syntax. At least you can use the code that complies with the language rules to let the program run first. With a general impression, building a small interest will go deeper. In case the first program gets stuck for 10 thousand times, it is estimated that anyone will lose interest.
I have read several books "C # advanced programming" version 2, 3, 4, and 6. I have read it all.
The first chapter is an overview of. net.
C # compilation is divided into two phases:
1. Compile the source code into Microsoft intermediate language (IL ).
2. CLR compiles IL into platform-specific code.
Then enter the C #[Syntax]
The related requirements of the program are nothing more:
1. end with a semicolon.
2. Letters are case sensitive.
3. The variable declaration must start with a letter or. You cannot use keywords as variable names.
4. C # Is a class C language ,{}.
The most common thing to write a program is to declare variables and assign values to variables.
Declare variables:
Data Type Variable Identification name;
We also learned the 'constant ', that is, const.
★A constant must be initialized during declaration. After its value is specified, it cannot be modified.
We also see the pre-defined data type of C.
At the beginning, we will introduce the first simple applet. I learned two IO/console input and output commands. Console. WriteLine (); and Console. ReadLine ();
With this, I can write the program, although it is very simple.
using System;namespace MySpaceProject{public class Hello{public static void Main(string[] args){Console.WriteLine("This is my first program!");Console.ReadKey();}}}
Variables are always declared, and output variables do not mean anything at all. What do we learn about programs? Apart from solving problems in our lives, do we want to develop powerful programs? Is it true that I learned to live in the DOS command line.
Next, I learned the flow control statements and loop statements.
Flow Control:
If (true ){
} Else if (true ){
} Else {
}
public static void Tif(){int x = 10;Tag:if(x>9 && x<=10){Console.WriteLine("x>9");x+=10;goto Tag;}else if(x>10 && x<21){Console.WriteLine("x in 10 and 20.");x+=100;goto Tag;}else if(x>110 && x<=120){Console.WriteLine("x < 120.");x -= 115;goto Tag;}else{Console.WriteLine("x is {0}.",x);}}
Switch (v ){
Case 1:
Console. WriteLine (1 );
Break;
Case 2:
Console. WriteLine (2 );
Break;
Default:
Console. WriteLine ("not 1 or not 2 ");
Break;
}
Loop statement: for/while/do... while/foreach
For (int I = 0; I <x. Length; I ++) {// readable, writable
}
While (true ){
Break;
}
Do {
Break;
} While (true );
Foreach (int I in ArrayOfInt) {// read-only for Loop
Console. WriteLine (I );
}
Jump statement:
Tag: Console. WriteLine ("goto in here! ");
Break; continue; goto Tag; return;
Enumeration:
Speaking of enumeration, this is very interesting. At the beginning, I thought this type was a little redundant, and it was the same as an integer. Later, I found out its charm when I used more times.
Enumeration: an integer defined by the user. Enumeration is equivalent to an alias for the whole.
Public enum ClubType {
Club, vipclub
}
Although we can:
Int x = 1;
If (x = 0 ){
Console. WriteLine ("Regular member ");
} Else if (x = 1 ){
Console. WriteLine ("VIP member ");
}
But for fear, the x variable is declared too early, and some operations occur during the period, resulting in a change in the result. In case of calculation, x = 5; in this case, how can I display the membership type? Although this phenomenon rarely happens, it rarely does not mean it will not happen.
Usage:
ClubType ct = ClubType. club;
Switch (ct ){
Case ClubType. club:
Console. WriteLine ("Regular member ");
Break;
Case ClubType. vipclub:
Console. WriteLine ("VIP member ");
Break;
Default:
Throw new Exception ("the member type is incorrect! ");
Break;
}
In this way, the chance of eliminating errors increases a lot. This is an amazing use of enumeration.
Public static void Tswitch () {ClubType ct = ClubType. club; // The default value is normal, because members of this level do not have special rights to Console. write ("Show your member number:"); string str = Console. readLine (); int v_count = 0; int a_count = 0; int k_count = 0; while (str! = "Exit") {int keys; if (Int32.TryParse (str, out keys) {switch (keys) {case 20: v_count ++; ct = ClubType. vipclub; break; case 30: a_count ++; ct = ClubType. allclub; break; case 40: k_count ++; ct = ClubType. kingclub; break; default: ct = ClubType. club; break ;}} switch (ct) {case ClubType. club: Console. writeLine ("\ r \ n Hello, welcome to the member center. Please stay in the member Hall and do not try to go anywhere. "); Break; case ClubType. vipclub: Console. WriteLine (" \ r \ n you are a VIP member, this is your {0} login. You can also enter your room in addition to the member Hall. Do not enter other rooms without authorization. Thank you for your cooperation. ", V_count); break; case ClubType. allclub: Console. WriteLine (" \ r \ n you are a [privileged] member, and this is your {0} login. You can go anywhere. ", A_count); break; case ClubType. kingclub: Console. WriteLine (" \ r \ n VIP member hello, this is your {0} login. You can go anywhere and instruct anyone to provide services for you. ", K_count); break;} Console. write ("\ r \ n show your member number:"); str = Console. readLine () ;}v_count = 0; a_count = 0; k_count = 0 ;}
String Conversion to enumeration:
MyEnum me = (MyEnum) Enum. Parse (typeof (MyEnum), "string", true );