Beginner with c # 7

zhaozj2021-02-08  213

1.7 statement (Statements)

C # borrows most of the statement methods of C / C , but there are still some attention to it. Others have changed.

Here, I only mention some C # unique stuff.

1.7.10 "foreach" statement

"Foreach" statement lists all elements in a collection and performs a series of operations on these elements. Still look at the example: * /

Using system;

Using system.collections;

Class test

{

Static void WriteList (arraylist list) {

Foreach (Object O IN LIST)

{

INT i = (int) O; // If it is for statement, here will be wrong here!

Console.writeline (0);

Console.WriteLine ( i);

}

}

Static void main () {

ArrayList List = New ArrayList ();

For (int i = 0; i <10; i )

List.Add (i);

WriteList (list);

}

}

/ * This example scans the entire "List" with "Foreach" and prints all the elements in "List". Sometimes still

It is very convenient.

1.7.15 Security Check Switch (The Checked and Unchecked Statements)

"Checked" and "unchecked" are used to control the inspection of mathematical operations and complete type conversions. "Checked" checks it

A violation in the domain in the role and throw an exception; "Unchecked" blocks all inspections. for example:*/

Using system;

Class test

{

Static int x = 1000000;

Static int y = 1000000;

Static int f () {

Checked {return (x * y);} // throw OverflowException

}

Static Int g () {

Unchecked {return (x * y);} // Return -727379968

}

STATIC int h () {

Return X * Y; // default.

}

Static void main () {

F (); // You can log out of this trip.

Console.writeline (g ());

Console.writeLine (H ());

}

}

/ *

There will be no errors in the compilation process. Because "Checked" and "Unchecked" work only when running. It is worth mentioning.

H (). Its default state and the status of the compiler currently default overflow check. However, the return result is affirmative and any of F () or g ().

Look at an example: * /

Using system;

Class test

{

Const int x = 1000000;

Const int y = 1000000;

Static int f () {

Checked {return (x * y);} // Compile Warning: Overflow

}

Static Int g () {

Unchecked {return (x * y);} // Return -727379968

}

STATIC int h () {

Return X * Y; // Compile Warning: Overflow}

Static void main () {

Console.writeline (f ()); // You can log out of this trip.

Console.writeline (g ());

Console.WriteLine (h ()); // You can log out of this trip.

}

}

/ * When F () and h () are evaluated, it will cause a compilation warning. In G (), because of "unchecked", blocking this police

. Note "Checked" and "unchecked" cannot operate the return value of the function! such as:*/

Class test

{

Static Int Multiply (int X, int y) {

Return x * y;

}

Static int f () {

Checked {Return Multiply (1000000, 1000000);} // with Return Multiply (1000000, 1000000);

} // has the same effect.

}

/ * In fact, everyone thinks about why M $ doesn't do this! The discussion of this content exceeds the scope of this article and the ability of the 俺.

In C #, all hexadecimal numbers are uint. If the forced type conversion can cause the compiler to report error. Use "unchecked"

Skip this mechanism and transform the UINT's hexadecimal number to int. Such as:*/

Class test

{

Public const Int Allbits = Unchecked ((int) 0xfffffffff);

Public const Int highbit = unchecked ((int) 0x80000000);

}

/ * All constants in the previous example are UINT, and more than int range, no "unchecked", this conversion will trigger a compiler error

error. Note: The "unchecked" operator is used above. Not a statement. However, in addition to one, "()", another

Outside "{}", almost the same. BTW, "Checked" is the same.

1.7.16 "LOCK" statement (The Lock Statement)

"LOCK" gets a mutually exclusive object lock. (I have checked some information, but I have not clearly explained, I will not introduce it)

转载请注明原文地址:https://www.9cbs.com/read-2828.html

New Post(0)