C # basic knowledge,

Source: Internet
Author: User

C # basic knowledge,

1. Two integer variables are exchanged and the third variable cannot be declared.

1             int n1 = 11;2             int n2 = 20;3             Console.WriteLine(n1 );4             Console.WriteLine(n2);5             n1 = n2 - n1;6             n2 = n2 - n1;7             n1 = n2 + n1;8             Console.WriteLine(n1 );9             Console.WriteLine(n2);

2. Declare a string type variable to receive user input variables

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace Escape Character 8 {9 class Program10 {11 static void Main (string [] args) 12 {13 Console. writeLine ("enter your name:"); 14 string name = Console. readLine (); 15 Console. writeLine ("Your name is {0}", name); 16} 17} 18}

3. escape characters in strings

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace Escape Character 8 {9 class Program10 {11 static void Main (string [] args) 12 {13 Console. writeLine ("today's weather is fine and good scenery everywhere! "); 14 Console. WriteLine (" The weather is fine today, \ n is wonderful everywhere! "); // \ N indicates a line break of 15 16 Console. WriteLine (" add a \ "Double quotation mark in this sentence! "); // \" The left slash represents an escape character. It is an English double quotation mark (17 18) string name1 = "Zhang San"; 19 string name2 = "Li Si "; 20 string name3 = "Wang Wu"; 21 string name4 = "Zhao six"; 22 Console. writeLine ("{0} \ t {1}", name1, name2); // \ t indicates a tab 23 Console. writeLine ("{0} \ t {1}", name3, name4); 24 Console. writeLine (name4 + "\ B"); // \ B indicates a backspace, which is placed at both ends of the string. 25 Console is useless. writeLine ("zhang \ bsan"); // place \ B in the middle of the string to delete a character 26 Console. writeLine ("I like big \ r \ n dog"); // \ r \ n in windows In this document, you can use line breaks. You can also use line breaks 27 string str1 = "today's weather is fine, \ r \ n good scenery everywhere "; 28 // we don't want to write two left slashes for each character, so we add @ cancel \ before double quotation marks to escape the character string so that it simply represents \ 29 System. IO. file. writeAllText (@ "E: \ 1.txt", str1); 30 string path =" E: \ a \ B \ c \ 1.txt"; // Output E: \ a \ B \ c \ 1.txt visible \ represents \ 31 // @ also represents the output of 32 Console in the original format. writeLine (@ "today's weather is fine and clear, 33 good scenery everywhere! "); 34 Console. WriteLine (path); 35 Console. WriteLine (" OK "); 36 Console. ReadKey (); 37} 38} 39}

4. convert Conversion

1             string str = "123";2             int num1 = Convert.ToInt32(str);3             Console.WriteLine(num1);4             double num2;5             num2 = Convert.ToDouble(str);6             Console.WriteLine(num2 );

5. when there are multiple projects in the solution, if the newly created Project (that is, right-click the solution to create a project) feels bad, and you want to rename the project, you will find that the namespace in it does not change with the rename, so you must delete and recreate it, because there is no trouble when there is an intersection between the two projects. In addition, do not use keywords for the project name. If you use them, you cannot use keywords in the project. If you do not want to use them, add a sequence number.

6. Determine a leap year

1 // determine the leap year 2 // The year can be fully divided by 400 or the year can be fully divided by 4, but cannot be fully divided by 100 3 Console. writeLine ("to determine whether the year is a leap year, enter a year:"); 4 int year = Convert. toInt32 (Console. readLine (); 5 bool B = year % 400 = 0 | year % 4 = 0 & year % 100! = 0; // when an expression contains | and &, the priority of the pair is higher than or. Of course, parentheses can be added, that is, good reading is not messy. We don't need to | and &, because the efficiency is a little lower than 6 Console. WriteLine (B );

7. Branch Structure

1/* branch structure: if-else 2 * select structure: if else-if switch-case 3 * loop structure: while do-while for foreach 4 */5 // if (Judgment condition) 6 // {7 // code to be executed; 8 //} 9 // first judge and then execute. A line of code may not execute 10 Console. writeLine ("Enter your keyboard time:"); 11 int mins = Convert. toInt32 (Console. readLine (); 12 if (mins> 60) 13 {14 Console. writeLine ("good husband, get up, eat shit! "); 15} 16 Console. ReadKey ();

 

1 // if-else executes at least one statement, which is used to determine two cases. 2 // if (true) 3 // {4 //; // Code 5 to be executed //} 6 // else 7 // {8 // if the conditions are not met, execute the statement 9 //} 10 // if else-if, multi-condition interval; if is true, execute the if statement, then, the following judgment will be interrupted. if an if statement is set up, the execution will jump out. Otherwise, the execution will continue. if else is set up, the execution will continue. if else is not set up, the 11 Console will not be executed. writeLine ("Enter your score:"); 12 double score = Convert. toInt32 (Console. readLine (); 13 14 if (score> = 90) 15 {16 Console. writeLine ("A"); 17} 18 else if (score> = 80) 19 {20 Console. writeLine ("B"); 21} 22 else if (score> = 70) 23 {24 Console. writeLine ("C"); 25} 26 else if (score> = 60) 27 {28 Console. writeLine ("D"); 29} 30 // else31 // {32 // Console. writeLine ("E"); 33 //} 34 else if (score <60) // This write also goes 35 {36 Console. writeLine ("E"); 37}

8. Exception capture

1 // throw an exception 2 Console. writeLine ("enter an integer:"); 3 try 4 {5 int num = Convert. toInt32 (Console. readLine (); 6 Console. writeLine ("the integer you entered is {0}", num); 7} 8 // remember that there cannot be any code between try and catch 9 catch 10 {11 Console. writeLine ("the value you entered is not an integer and cannot be converted to an integer of the int type! "); 12}

 

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.