Visual C # getting started with programming

Source: Internet
Author: User
Program The activity is through Statement ( Statement ) . C # supports several different statements. Many statements are defined in the form of embedded statements. Block ) You can write multiple statements in the context where only one statement can be used. A block consists of a list of statements enclosed in braces. Statement (declaration statement) ) Declare local variables and constants. Expression Statement ( Expression statement ) Used for Operation expressions. The expression can be used as a statement to use 3, including method call, object allocation using the new operator, and assignment using the "=" and compound assignment operators, and the increment and decrement operations using the "++" and "--" operators.
Selection statement
) Used to select one of several possible statements based on the value of an expression. This group of statements includes if and switch statements.
Iteration statement
) Used to repeatedly execute embedded statements. This group of statements includes the while, do, for, and foreach statements. Jump Statement (jump statement ) It is used for transfer program control. This group of statements includes break, continue, Goto, throw, and return statements. The try-catch statement is used to capture exceptions that occur during block execution. In addition, the try-finally statement is used to specify a termination Code Block, whether an exception occurs or not, it is always executed. The checked and unchecked statements are used to control integer arithmetic operations and overflow checks of the conversion. The lock statement is used to obtain the mutex lock of a given object, execute the statement, and then release the lock. The Using statement is used to obtain a resource, execute a statement, and process the resource. Table 1.5 lists the C # statements and provides examples one by one. Table 1.5 C # statements
Sentence Example
Local variable Declaration Static void main () {int A; int B = 2, c = 3; A = 1; console. writeline (A + B + C );}
Local constant Declaration Static void main () {const float Pi = 3.1415927f; const int r = 25; console. writeline (pI * r );}
Expression statement Static void main () {int I; I = 123; // expression statement console. writeline (I); // expression statement I ++; // expression statement console. writeline (I); // expression statement}
If statement Static void main (string [] ARGs) {If (ARGs. length = 0) {console. writeline ("no arguments");} else {console. writeline ("one or more arguments ");}}
(Continued table)
Sentence Example
Switch statement Static void main (string [] ARGs) {int n = args. length; Switch (n) {Case 0: console. writeline ("no arguments"); break; Case 1: console. writeline ("one argument"); break; default: console. writeline ("{0} arguments", n); break ;}}
While statement Static void main (string [] ARGs) {int I = 0; while (I <args. Length) {console. writeline (ARGs [I]); I ++ ;}}
Do statement Static void main () {string s; do {S = console. Readline (); If (s! = NULL) console. writeline (s);} while (s! = NULL );}
For statement Static void main (string [] ARGs) {for (INT I = 0; I <args. length; I ++) {console. writeline (ARGs [I]);}
Foreach statement Static void main (string [] ARGs) {foreach (string s in ARGs) {console. writeline (s );}}
Break statement Static void main () {While (true) {string S = console. Readline (); If (S = NULL) break; console. writeline (s );}}
Continue statement Static void main (string [] ARGs) {for (INT I = 0; I <args. length; I ++) {If (ARGs [I]. startswith ("/") continue; console. writeline (ARGs [I]);}
GOTO statement Static void main (string [] ARGs) {int I = 0; goto check;
Loop:
Console. writeline (ARGs [I ++]); check: if (I <args. Length) goto loop ;}
(Continued table)
Sentence Example
Return Statement Static int add (int A, int B) {return a + B;} static void main () {console. writeline (add (1, 2); return ;}
Throw and try statements Static double divide (Double X, Double Y) {If (y = 0) throw new dividebyzeroexception (); Return x/y;} static void main (string [] ARGs) {try {If (ARGs. length! = 2) {Throw new exception ("two numbers required");} Double X = double. parse (ARGs [0]); Double Y = double. parse (ARGs [1]); console. writeline (divide (x, y);} catch (exception e) {console. writeline (E. message );}}
Checked and unchecked statements Static void main () {int I = int. maxvalue; checked {console. writeline (I + 1); // exception} unchecked {console. writeline (I + 1); // overflow }}
Lock statement Class account {decimal balance; Public void withdraw (decimal amount) {lock (this) {If (amount> balance) {Throw new exception ("insufficient funds ");} balance-= amount ;}}}
Using statement Static void main () {using (textwriter W = file. createtext ("test.txt") {W. writeline ("line one"); W. writeline ("line two"); W. writeline ("line three ");}}

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.