C # borrows most of the statement methods of C + +, but there are still some noteworthy areas. There are other areas where changes have been made.
Here, I only mention some C # special stuff.
1. 7. "foreach" statement
The foreach statement enumerates all the elements within a collection and performs a series of actions on those 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 this is a for statement, there will be an error!
Console.WriteLine (0);
Console.WriteLine (++i);
}
}
static void Main () {
ArrayList list = new ArrayList ();
for (int i = 0; i < i++)
List. ADD (i);
Writelist (list);
}
}
/* This example uses "foreach" to scan the entire "list" and print all the elements in "list". Sometimes it's
It's very convenient.
1. 7. 15 Security Check switch (the checked and unchecked statements)
The "checked" and "unchecked" statements are used to control the checking of mathematical operations and full type conversions. "Checked" Check it
A possible violation in the domain in which it is acting and throws an exception, while "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);}//Back-727379968
}
static int H () {
return x * y; The default state.
}
static void Main () {
F (); You can log off and try this trip.
Console.WriteLine (G ());
Console.WriteLine (H ());
}
}
/*
No errors occur during compilation. Because "checked" and "unchecked" work only at run time. It's worth saying.
H (). Its default state is related to the state of the compiler's current default overflow check. But the results returned are definitely the same as either f () or g ().
One more 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);}//Back-727379968
}
static int H () {
return x * y; Compiler warning (Compile warning): Overflow (overflow)
}
static void Main () {
Console.WriteLine (F ()); You can log off and try this trip.
Console.WriteLine (G ());
Console.WriteLine (H ()); You can log off and try this trip.
}
}
/* when F () and H () are evaluated, a compilation warning is generated. and in G (), because of the "unchecked", shielding the police
Sue. Note that both "checked" and "unchecked" cannot operate on the return value of a function! For example: * *
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, we think a little bit about why m$ did not do so! The discussion of this content is beyond the scope of this article and my ability to OH.
In C #, all hexadecimal numbers are uint. If you use coercion type conversion, the compiler will cause an error. With "Unchecked" you can
Skip this mechanism and convert the hexadecimal number of uint to int. such as: * *
Class Test
{
public const int allbits = unchecked ((int) 0xFFFFFFFF);
public const int highbit = unchecked ((int) 0x80000000);
}
/* All constants in the above example are uint, and they exceed the range of int, there is no "unchecked", this conversion throws a compiler error
Miss. Note: the "unchecked" operator is used above. is not a statement. But there is a "()" in between them, and another
"{}" is almost the same. BTW, "checked" is the same.
1. 7. "Lock" statement (the lock statement)
Lock gets an object lock that is mutually exclusive. (I have checked some information, but have no clear explanation, temporarily do not introduce)
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