I have been studying C # for several weeks in my junior year. I hope to share with you the following:
1And naming rules for variables:
Currently, two naming methods are generally used: ① pascalcase: it specifies that the first letter of each word is written up
② Camel case: it specifies that except the first word, the first letter of each other word is uppercase.
Currently, the following dashes start out of date.
2,String Literal Value
when a string contains an escape character, use a specified string that is verbatim.
for example, "This address is C: \ My \ wtq "is equivalent to @" this address is c: \ My \ wtq "
3 , declared value assignment in the variable
when defining a variable in C #, you must assign a value before using it. Otherwise, a compilation error is reported.
3.3.1
① user input
string username = console. readline (); this statement returns a string
② type conversion
double firstnum = convert. todouble (console. readline ();
3.3.2 value assignment operators
"+" and "+ =" can be used for links between strings.
3.4 namespace
① In. NET provides the Program Code container method
② is also used. A way to classify items in netframework. Most items are Type Definitions
4 , process control
4.1 boolean logic
bool var1, var2, var3;
var1 = var2 = var3;
var1 = var2 = ture;
bool type operations can only be performed = or! = Operation
& | and | the basic calculation results are the same during running, but the execution performance is different.
when expression A & B is executed, when expression A is false, you do not need to judge whether the value of B is true or false. In this case, the value of the entire expression is false. This can save time, and when the expression A & B is executed, the operations A and B must be judged to be true and false. This is not very efficient.
when expression a | B is executed, when expression A is true, it is not necessary to judge whether the value of B is true or false. In this case, the value of the entire expression is true. This saves time, and when the expression a | B is executed, the operations A and B must be true and false. This is not very efficient.
4.2 GOTO statement
usage:
lable:
.
.
.
goto lable;
the GOTO statement should be used as few as possible. If the program logic is relatively simple, it is more efficient to use the GOTO statement. However, if the program is more complex, the readability of the entire program is not very good, making programming more difficult.
4.3
The branch contains the following three cases:
① Condition Statement:? B: C;
② If statement
③ Switch statement
For the ① Condition Statement, it returns a value.
For example, string d =? B: C if A is true, B is assigned to D;
For the if Statement of ②, it does not return results, and it only has true or false.
Use if () whenever possible ()
{
}
Else {
}
If there are many conditions, you can use
If ()
.{
}
Else if ()
{
}
Else if ()
{
}
For ③ switch (). A can be a literal or a constant.
When defining a constant, use const int inttwo = 2;
Such as statements;
Switch ()
{
Case A:; break \ return;
Case B:; break \ return;
Case C:; break \ return;
Defaule:; break \ rturn;
}
4.4 loops
do {
}while ()
while ()
{
}< br>
for (Int = I; I <10; I ++)
this variable I can only be used in the loop body, it cannot be used for statements other than loops.
Note: it is illegal for a goto to enter the loop from the external.
4.5 program robustness (this is only an example):
do {
A = console. Readline ();
If (A <B)
Console. writeline ("the data you entered is incorrect. Please enter it again ");
} While (A <B );
Complete program:
Static void main (string [] ARGs)
{
Int firstnum, secondnun = 20;
Do
{
Firstnum = convert. toint32 (console. Readline ());
If (firstnum <secondnun)
Console. writeline ("the number your enter is error. \ nplease enter
Another number ");
} While (firstnum <secondnun );
The running result is as follows:
5.More information about variables
5.11 Implicit conversion Generally, the data type with a small number of bytes is converted to a data type with a large number of bytes (of course, it can be equal ). For example: Ushort And Char (Equal byte) Int And Double (small-> large)
5.12 Explicit conversions
Explicit conversions generally convert a type with a large number of bytes to a type with a small number of bytes.
Example: byte destinationbyte;
Short sourceshort
Destinationbyte = (byte) sourceshort;
Note: In this case, the value assignment may cause result overflow;
Example: sourceshort = 281;
If destinationbyte = (byte) sourceshort is executed, the value of destinationbyte will be changed to 25.
Because 281 = 100011001
25 = 00011001
255 = 01111111
At this time, we can take appropriate measures to solve the problem.
For example: checked (expression)
Or unchecked (expression)
Destinationbyte = checked (byte) sourceshort)
Another method is to modify the configuration application:
① Click solution Resource Manager.
Set
Click "advanced" on the right and click "operate ".
5.1.3 use the convert command for explicit conversion
Int varint;
Short varshort = 10;
Float varfloat = 20;
Varint = varshort * (short) varfloat; // The result of this sentence is a value of the int type. If varint is of another type, an error is returned.
Vardouble = convert. todouble ("125 ");
Console. writeline (varint );
5.2 Complex Variable types ① Enumeration type Form: Enum typename {value1 , Value2 , Value3 , .. Valuen } This type uses one Int Type Storage If you want to change the storage, you can change the definition: Enum typename : Underlyingtype ( Int ) {Value1 , Value2 , Value3 , .. Valuen }Frequent outlets Enum typename {value1 = actualval , Value2 , Value3 = value1 , Value4 , . Valuen } In this case Value2 = value4 This kind of detour may easily lead to errors. Enum {value1 = value2value2 = value1} The enumeration type definition is generally placed in the namespace. Namespace test { Enum direction { North, South, East, West } Class Program { } Structure ② Struct typename {} ③ Array declaration: (1) One-dimensional array: Definition: for example: Int [] varint; Initialization: Const int varint = 5 ; Int [] arrary = {5, 3, 9, 4, 3}; int [] arrary = new int [5]; By default, each element is 0. Int [] arrary = new int [varint]; By default, each element is 0. It cannot be written like this during definition : Int [] array; Array = new int [5]; Because the array size must be allocated when it is defined. When assigning an initial value to an array, note that the array size must match the number of elements. For example: Int [] array = new int [5] {1, 2, 3, 4, 5 }; Int [] array = new int [5] {1, 2, 3 }; This is wrong Yes Foreach Loop to read the value of the array element, but cannot be modified. In contrast, For You can read and modify information in a loop. Foreach (INT VaR in array) { Console. writeline (VAR ); } (2) Multi-dimensional array Definition: Double [,] hillheight = new double [3, 4]; Initial values: Double [,] hillheight = new double [2, 3] {4, 2, 3}, {4, 5, 6 }}; Or Double [,] hillheight = {, 3}, {, 6 }}; (3) Array (the number of elements in each row can be different. Strictly speaking, it is a type of multi-dimensional array. ) Initial Values: ① Int [] [] var2; Var2 = new int [2] []; Var2 [0] = new int [4]; Var2 [1] = new int [3]; Var2 [0] [1] = 2 ;// This is different from multi-dimensional arrays because Var2 [1, 2] ② Int [] [] var2 = {New int [1] {2}, new int [2] {3, 4 }}; ③ Int [] [] var2; Var2 = new int [2] [] {New int [1] {2}, new int [2] {3, 4 }}; Also available Foreach Read the value of the array by using nested Foreach () foreach (INT [] vararray in var2) { Foreach (INT var3 in vararray) { Console. writeline (var3 ); } } 5.5 String processing ① String String Can be converted CharArray For example: String mystring = "a string "; Char [] mychar = mystring. tochararray (); Foreach (char CH in mychar) { Console. writeline (CH ); } Console. writeline (mychar. Length );// The size of the output array. ② Convert string to lowercase For example: String userresponse = console. Readline (); If (userresponse. tolower () = "wtq ") { Console. writeline (" You are great ");} ③ Convert string to uppercase For example: String userresponse = console. Readline (); If (userresponse. toupper () = "wtq ") { Console. writeline (" You are great "); } ④ Delete extra spaces For example : String userresponse = console. Readline (); Userresponse = userresponse. Trim (); If (userresponse. toupper () = "wtq ") { Console. writeline (" You are great "); } ⑤ Delete additional characters For example: Char [] trimchars = {'', 't', 'q '}; String userresponse = console. Readline (); Userresponse = userresponse. Trim (trimchars ); If (userresponse = "W ") { Console. writeline (" You are great "); } ⑥ Delete the leading or trailing spaces of a string <String>. trimstart (), <string>. trimend For example: String userresponse = console. Readline (); Userresponse = userresponse. trimstart (); If (userresponse = "W ") { Console. writeline (" You are great "); } 7. <String>. padleft And <String>. padright Add spaces on the left or right of the string. For example: String mystring = "aligned "; Mystring = mystring. padleft (10 ); Console. writeline (mystring ); } Note: You can also add other characters before the string. For example: String mystring = "aligned "; Mystring = mystring. padleft (10, '+ '); Console. writeline (mystring ); Delimiter <String>. Split () For example: String mystring = "this is a test "; Char [] separator = {'', 'I '}; String [] mywords; Mywords = mystring. Split (separator ); Foreach (string word in mywords) { Console. writeline (Word ); } Reverse returns the string in reverse order. Method 1: String text = "wtqwtqwtq "; Char [] carray = text. tochararray ();
String reverse = string. Empty ;// Equivalent Reverse Initial Value assignment
For (INT I = carray. Length-1; I>-1; I --)
{
Reverse + = carray [I];
} Console. writeline (reverse ); Method 2: String text = "wtqwtqwtq "; Char [] chararray = text. tochararray (); Array. Reverse (chararray ); Console. writeline (chararray );