General understanding of C #(7-8 sets and foreach statements/structures)

Source: Internet
Author: User

7. Set and foreach statements

C # provides a shortcut for a for loop, and it also promotes more consistent set classes:

In Java or C ++:

1. while (! Collection. isEmpty ())

{

Object o = collection. get ();

Collection. next ()

//...

2. for (int I = 0; I <array. length; I ++)

//...

In C:

1. foreach (object o in collection)

//...

2. foreach (int I in array)

//...

The for loop of C # will work on the set object (array implements a set ). The collection object has a GetEnumerator () method, which returns an Enumerator object. The Enumerator object has a MoveNext () method and a Current attribute.


8. Structure

Consider the structure of C # as a more elegant language type system, rather than a concept that "you can use it to write truly efficient code if you need it.

In C ++, both structures and classes (objects) can be distributed on stacks or stacks. In C #, the structure is always created on the stack, and the class (object) is always created on the stack. The structure can actually generate more efficient code:

Public struct Vector

{

Public float direction;

Public int magn.pdf;

}

Vector [] vectors = new Vector [1000];

This will allocate 1000 vectors to a space, which is much more efficient than declaring a Vector as a class and instantiating 1000 Independent Vectors Using A for loop. [Note: If you suspect that the original article is incorrect, you may intentionally omit this sentence, but it should not affect your understanding of this article ]:

Int [] ints = new ints [1000]; // [Note: the code here is incorrect. It should be int [] ints = new int [1000];]

C # allows you to expand the basic type set built in the language. In fact, all basic types of C # are implemented in structure. The int type is only the alias of the System. Int32 structure, and the long type is the alias of the System. Int64 structure. Of course, these basic types can be specially processed by the compiler, but the language itself is no different ]. In the next section, we can see how C # achieves this.

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.