The difference between a C # array's properties, a foreach traversal, a jagged array and a rectangular array

Source: Internet
Author: User
Tags foreach array definition arrays


In this lesson we are going to learn the properties of arrays, foreach usage, the jagged array I mentioned in the last lesson, I'll show you a few examples of how the jagged array definition has been better understood than the regular two-dimensional array (rectangular array), and we'll also learn the common methods of arrays in the next section.



Now let's take a look at the properties of the array, what is the attribute? I will give you a detailed explanation when you learn the class later. Now I give you a real-life example to help you understand what is called attributes, which are generally nouns that represent the attributes that their owners (i.e. objects) have, such as the color, size (attributes) of a rag (object), And the next section will learn the array method is generally a verb, such as the function of Dishcloth, Dishcloth (object) to clear (method) desktop, vehicles, ground (the object of these actions can also be likened to later to learn parameters), now you will remember that attributes are descriptive object characteristics, is generally a noun, the method is to describe the object function, It's usually a verb. What are the attributes of that array?



Attributes of an array: the capacity of the Array.Length array



With this property, we can get the capacity values that the array object allows to store, that is, the length of the array, the number of elements, this relatively good understanding of the array and other attributes, such as the number of dimensions of the array, the use of attributes is relatively simple, learn one, the other formats are basically consistent, here we do not give examples.



In the last example of the previous section we used the index to get the value of the array elements Array[index], but when the number of dimensions and capacity of the array, the use of index this method will be complicated, not only the large amount of code will also reduce programming efficiency, in response to this problem C # provides A foreach statement, designed to read all the elements in a collection/array, which we call the traversal. The grammar is written as follows:



Traversal array: foreach (Type objname in Collection/array)



This statement examines each stored variable value in the array, and takes it out one at a time, where the type is the data type that the array object you want to read will be stored in the objname variable, and objname is the variable name that defines a type. Represents each element that is taken from a collection and array (Collection/array), and the Collection/array is the object to be accessed. By writing only one foreach in this way, you can traverse an array of all the dimensions except the jagged array.



Note: The type of the objname data type must be the same or larger than the Collection/array object.



Here we give an example of a foreach and a For traversal rule array, which involves a method of getting the dimensions of an array, comparing the advantages of foreach in a one-time traversal of the rule array.



int[,,] a = new int[2, 2, 2] { {{ 1, 2 }, { 3,4}},{{ 5, 6 }, { 7,8}} };//Define a 3-dimensional array a with 2 rows, 2 columns and 2 depths
for (int i = 0; i <a.GetLength (0); i ++) // Use Array.GetLength (n) to get the number of dimensions in the array [0,1 ,,, n], where 0 is the row, 1 column, n means this array is n + 1 dimensions
{
for (int j = 0; j <a.GetLength (1); j ++)
{
for (int z = 0; z <a.GetLength (2); z ++) // 2 represents the number of elements in the depth. If the array has n dimensions, you must write n for loops.

{
Console.WriteLine(a[i,j,z]);
}
}
}





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.