Beginner with c #7

Source: Internet
Author: User

1. 7 Statement (Statements)

C # borrowed most of the c/c ++ statement methods, but there are still some noteworthy points. There are also some changes.
Here, I only mention some special c # stuff.

1. 7. 10 "foreach" statements
The "foreach" statement lists all elements in a set and performs a series of operations on these elements. Let's take a 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 a for statement, an error will be reported 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 );
}
}
/* In this example, "foreach" is used to scan the entire "list" and print out all the elements in "list. Sometimes
Very convenient.

1. 7. 15 The checked and unchecked statements)
The "checked" and "unchecked" statements are used to control mathematical operations and check the complete type conversion. "Checked" to check it
And throws an exception. "unchecked" blocks all checks. 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 status.
}
Static void Main (){
F (); // you can log out of this line and try again.
Console. WriteLine (G ());
Console. WriteLine (H ());
}
}

/*
No errors occur during compilation. Because "checked" and "unchecked" only work at runtime. It is worth noting that
H (). Its default status is related to the current default overflow check status of the compiler. However, the returned results must be the same as any one in F () or G.
Let's look at another example :*/

Using System;
Class Test
{
Const int x = 1000000;
Const int y = 1000000;
Static int F (){
Checked {return (x * y);} // compiler warning (Compile warning): overflow (overflow)
}
Static int G (){
Unchecked {return (x * y);} // return-727379968
}
Static int H (){
Return x * y; // compiler warning (Compile warning): overflow)
}
Static void Main (){
Console. WriteLine (F (); // you can log out of this row and try again.
Console. WriteLine (G ());
Console. WriteLine (H (); // you can log out of this row and try again.
}
}

/* When F () and H () are evaluated, a compilation warning is triggered. In G (), the police are blocked because of "unchecked ".
. Note that neither "checked" nor "unchecked" can operate the return value of the function! For example :*/
Class Test
{
Static int Multiply (int x, int y ){
Return x * y;
}
Static int F (){
Checked {return Multiply (1000000,100 0000);} // with return Multiply (1000000,100 0000 );
} // Has the same effect.
}
/* In fact, let's take a look at why m $ didn't do this! The discussion of this content is beyond the scope of this article and beyond the capabilities of Alibaba Cloud.

In c #, all hexadecimal numbers are uint. If forced type conversion is used, the compiler reports an error. With "unchecked", you can
Skip this mechanism and convert the hexadecimal number of uint to int. For example :*/

Class Test
{
Public const int AllBits = unchecked (int) 0 xFFFFFFFF );
Public const int HighBit = unchecked (int) 0x80000000 );
}

/* In the above example, all constants are uint, which is beyond the int range and does not have "unchecked". This conversion will cause a compiler error.
Error. Note: The "unchecked" operator is used above. Not a statement. However, one of them uses "()", and the other uses
"{}" Is almost the same. BTW, "checked.

1. 7. 16 "lock" statement (The lock statement)
"Lock" gets a mutually exclusive object lock. (I have checked some materials, but I have not clearly stated them. I will not introduce them for the moment)

Related Article

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.