Objective C # Chapter 1

Source: Internet
Author: User

After nearly a month, I finally completed the first chapter of learning + translation. The content in Chapter 1 is relatively basic, and most of the content can be seen as a lecture on how to develop good programming habits. In addition to the gethashcode () section, other content is commonly used programming skills. Distinguishing between reference and value types and the relationship between four equals () is also the basic knowledge of. net.

When comparing foreach and for loops in item 11: prefer foreach loops, loop 3 in the book will judge whether the array is out of bounds during the loop, which may be a problem. Msil disassembler is used to check the msil of loop 3. The check for out-of-boundary behaviors described in this document are not found. The biggest difference between them and loop 2 lies in the judgment of cyclic variables. The example is as follows:

Int [] Foo = new int [100];
// Loop 1
Foreach (int I in Foo)
{
Console. writeline (I. tostring ());
}
// Cycle 2
For (INT Index = 0; index <Foo. length; index ++)
{
Console. writeline (FOO [Index]. tostring ());
}
// Loop 3
Int Len = Foo. length;
For (INT Index = 0; index <Len; index ++)
{
Console. writeline (FOO [Index]. tostring ());
}

The following items are available now and found in the manual ....
Through msil, we can see that loop 3 does this:

Il_001d: LDC. i4.s 100
Il_001f: BLT. s il_0014

Constant 100 is pushed into the stack with a 4-byte integer, and then compared with the cyclic variable. If the value is smaller than, It is redirected to il_0014, that is, the start part of the loop. No behavior is found to determine whether the array is out of bounds.
Loop 1 and loop 2 do the following in this function:

Il_0029: ldlen
Il_002a: Conv. I4
Il_002b: BLT. s il_0017

Obtain the array length, convert it to a 4-byte integer, and then compare it. The red part is where they are. This is also consistent with the test results.

Finally, I hope you will give more valuable comments, 3x. Click the link below to go to the first chapter of the directory.

Valid C # directory

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.