C # Learning diary 29 ---- two-dimensional indexer and foreach traverse Indexer

Source: Internet
Author: User

C # Learning diary 29 ---- two-dimensional indexer and foreach traverse Indexer

At the end of the previous article, I left a few questions, because the power failure was not solved. This article continues with the previous article.Click here to return to the previous article

Question 1:

The array has multiple dimensions. Can the indexer be multidimensional ???

The indexer can be multidimensional. The indexer we defined in the previous article is only a one-dimensional indexer, which can be defined like an array. For example, we index the seat number of a screening room in a cinema. The first column in the first row is No. 1, and the second column is No. 2 ...... As follows:

Using System; using System. collections. generic; using System. linq; using System. text; namespace Test1 {// defines the cinema class to contain a two-dimensional array and a two-dimensional accessors class cinema {// defines a two-dimensional array private string [,] seat = new string [5, 5]; // defines a two-dimensional accessors public string this [uint a, uint B] {get {return seat [a, B];} set {seat [, b] = value ;}} class Program {static void Main (string [] args) {cinema movioom = new cinema (); // instantiate // for Loop traversal write for (uint I = 1; I <5; I ++) {for (uint j = 1; j <5; j ++) {movioom [I, j] = + I + row + j + column;} // read for (uint I = 1; I <5; I ++) {for (uint j = 1; j <5; j ++) {Console. writeLine (movimirrored [I, j] ++ (I-1) * 4 + j) + number );}}}}}


Result:

 

This is the case with the two-dimensional indexer, and so on for other multi-dimensional data.

 

Question 2:

The array can be traversed easily and quickly using the foreach statement. Can the indexer also use the foreach statement to traverse the array ???

This is also possible. When solving this problem, it is necessary to clarify the steps and principles of foreach execution.

Foreach statement:

In C #, the compiler converts the foreach statement into the methods and attributes of the IEnumerable interface, for example:

String [] str = new string [] {HC1, HC2, HC3, HC4}; // define an array foreach (string I in str) // use foreach to traverse {Console. writeLine (I );}


However, the foreach statement is parsed as the following code segment.

Using System; using System. collections. generic; using System. linq; using System. text; using System. collections; // note that this namespace is added. Otherwise, the namespace Example {class Program {static void Main (string [] args) of the IEnumerator class is not available) {string [] str = new string [] {HC1, HC2, HC3, HC4}; // defines an array // call the GetEnumerator () method, obtain an enumerative IEnumerator per = str. getEnumerator (); // In the while LOOP, As long as MoveNext () returns true, the loop continues. moveNext () {// use the Current attribute to access the element string p = (string) per in the array. current; Console. writeLine (p );}}}}

 

The results are the same:

 

We can view the definition of string and find that string inherits from the IEnumerable interface. The IEnumerable interface only has one method GetEnumerator (); (this method has been implemented in the string class) the function of this method is to return an enumerative IEnumerator of the loop access set. We are converting the definition of IEnumerator. It is also an interface with only three methods declared, current (get the Current element in the set), MoveNext (push the number of enumerations to the next element of the Set, return true successfully, return false beyond the end ), reset (set the number of enumerations to its initial position before the first element in the set). That is to say, if the GetEnumerator method is not implemented in our custom class, and the Current and MoveNext methods cannot be traversed using the foreach statement.

 

The foreach statement traverses the custom class:

The example of the cinema above, but this time we don't need for loop output, but implement the foreach statement traversal output, as shown below:

Using System; using System. collections. generic; using System. linq; using System. text; using System. collections; // Add this namespace Test1 {// define cinema to inherit from the IEnumerable interface to implement the GetEnumerator () function class cinema: IEnumerable {// define a two-dimensional array private string [,] seat = new string [5, 5]; // defines the seat number static public int index =-1; // defines a two-dimensional indexer public string this [uint, uint B] {get {return seat [a, B];} set {seat [a, B] = value ;} // set accessors with value parameter} // implement the GetEnumerator method public IEnumerator GetEnumerator () {return new ienumerator (seat ); // use the constructor to pass in the seat parameter} // inherits the IEnumerator interface and implements the private class ienumerator: IEnumerator {private string [,] seats; // assign the passed seat array to the public ienumerator (string [,] s) {seats = s ;} // define the Current read-only attribute public object Current {// calculate the coordinates of the Array Based on the seat number, that is, the physical location get {return seats [1 + (index/4 ), (index % 4) + 1] ;}// defines the downward moving rule public bool MoveNext () {index ++; // index the location of the next seat number if (index <= 15) {return true;} else return false;} // it is not implemented because this method is not used in this program, but it must be written in; otherwise, the public void Reset () {}} class Program {static void Main (string [] args) {cinema movioom = new cinema (); // instantiate // for loop index write for (uint I = 1; I <5; I ++) {for (uint j = 1; j <5; j ++) {movioom [I, j] = + I + row + j + column;} // call the foreach statement to traverse the output foreach (string I in movioom) {Console. writeLine (I ++ (cinema. index + 1) + number );}}}}


Result:

 

 

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.