Home Backend Development C#.Net Tutorial C# Learning Diary 29----Two-dimensional indexer and foreach traversal indexer

C# Learning Diary 29----Two-dimensional indexer and foreach traversal indexer

Jan 21, 2017 pm 03:43 PM

At the end of the previous article, I left a few questions, which were not solved because the power was about to be cut off. In this article, we will continue the content of the previous article. Click here to return to the previous article

Question 1:

Arrays have multiple dimensions, can indexers also be multi-dimensional? ? ?

Indexers can be multi-dimensional. The indexer we defined in the previous article is only a one-dimensional indexer. Like an array, a multi-dimensional indexer can be defined. For example, if 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 in the first row is No. 2... as follows:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Test1  
{//定义cinema类包含一个二维数组与一个二维访问器  
    class cinema  
    {//定义一个二维数组  
        private string[,] seat = new string[5, 5];  
    //定义一个二维访问器  
        public string this[uint a, uint b]  
        {  
            get { return seat[a, b]; }  
            set { seat[a, b] = value; }  
          
        }  
      
    }  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            cinema movieroom = new cinema();//实例化  
            //for循环遍历写入  
            for (uint i = 1; i < 5; i++)  
            {  
                for (uint j = 1; j < 5; j++)  
                {  
                    movieroom[i, j] = "第" + i + "排 第" + j + "列";  
                }  
            }  
            //for循环遍历读出  
            for (uint i = 1; i < 5; i++)  
            {  
                for (uint j = 1; j < 5; j++)  
                {  
                    Console.WriteLine(movieroom[i,j]+"\t"+((i-1)*4+j)+"号");               
                  
                }  
              
            }  
        }  
    }  
}
Copy after login

Result:

C# Learning Diary 29----Two-dimensional indexer and foreach traversal indexer

This is the case for two-dimensional indexers. Other multi-dimensional indexers are similar and will not be introduced.

Question 2:

The array can be traversed simply and quickly using the foreach statement. Can the indexer also be traversed using the foreach statement? ? ?

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

foreach statement:

The compiler in C# will The statement is converted into the methods and properties of the IEnumerable interface, such as:

string[] str = new string[] { "HC1", "HC2", "HC3", "HC4" };//定义一个数组  
            foreach (string i in str)//使用foreach遍历  
            {  
                Console.WriteLine(i);  
            }
Copy after login

However, the foreach statement will be parsed into the following code segment.

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Collections; //注意添加这个命名空间,否则没有IEnumerator这个类  
  
namespace Example  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            string[] str = new string[] {"HC1","HC2","HC3","HC4" }; //定义一个数组  
          //调用GetEnumerator()方法,获得数组的一个枚举  
            IEnumerator per = str.GetEnumerator();  
          //在while循环中,只要MoveNext()返回true,就一直循环下去  
            while (per.MoveNext())  
            {  
                //用Current属性访问数组中的元素  
                string p = (string)per.Current;  
                Console.WriteLine(p);  
            }  
        }  
    }  
}
Copy after login

The results are the same:

C# Learning Diary 29----Two-dimensional indexer and foreach traversal indexer


We looked at the definition of string and found that string inherits from the IEnumerable interface, and the IEnumerable interface There is only one method GetEnumerator(); (this method has been implemented in the string class). The function of this method is to return an enumerator IEnumerator that iterates through the collection. We are changing the definition of IEnumerator, which is also an interface. There are only three method declarations, Current (get the current element in the collection), MoveNext (advance the enumerator to the next element of the collection, return true if successful, return false if it exceeds the end), Reset (set the enumerator to it The initial position, which is before the first element in the collection), that is to say, if the GetEnumerator method, as well as the Current and MoveNext methods are not implemented in our custom class, we cannot use the foreach statement to traverse.

foreach statement traverses custom classes:

It’s still the movie theater example above, but this time we don’t use a for loop to output, but implement a foreach statement to traverse the output, as follows:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Collections; //添加这个很有必要  
  
namespace Test1  
{//定义cinema继承IEnumerable接口实现GetEnumerator()功能  
    class cinema:IEnumerable  
    {//定义一个二维数组  
        private string[,] seat = new string[5, 5];  
      //定义座位号码  
        static public int index=-1;  
    //定义一个二维索引器  
        public string this[uint a, uint b]  
        {  
            get { return seat[a, b]; }  
            set { seat[a, b] = value; }//set访问器自带value参数  
          
        }  
        //实现GetEnumerator方法  
        public IEnumerator GetEnumerator()  
        {  
            return new ienumerator(seat); //利用构造方法传入seat参数  
        }  
        //由于上面返回值的需要所以继承接口IEnumerator并实现方法  
       private class ienumerator:IEnumerator  
        {  
            private string[,] seats; //将传入的seat数组赋给它  
            public ienumerator(string[,] s)  
            {  
                seats = s;   
            }  
           //定义Current的只读属性  
            public object Current  
            {  //根据座位号推算数组的坐标也就是物理位置  
               get { return seats[1+(index/4), (index%4)+1]; }  
              
            }  
           //定义向下移动的规则  
            public bool MoveNext()  
            {  
                index++; //索引下一个座位号的位置  
                if (index <= 15)  
                {  
                    return true;  
                }  
                else  
                    return false;  
              
            }  
           //因为这个程序中用不到这个方法所以不实现,但是必须得写上否则会报错  
            public void Reset()  
            {   
                          
            }  
          
        }  
      
    }  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            cinema movieroom = new cinema();//实例化  
            //for循环索引写入  
            for (uint i = 1; i < 5; i++)  
            {  
                for (uint j = 1; j < 5; j++)  
                {  
                    movieroom[i, j] = "第" + i + "排 第" + j + "列";  
                }  
            }  
        //调用foreach语句遍历输出  
            foreach (string i in movieroom)  
            {  
                Console.WriteLine(i+"\t"+(cinema.index+1)+"号");  
                  
            }  
        }  
    }  
}
Copy after login

Result:

C# Learning Diary 29----Two-dimensional indexer and foreach traversal indexer

The result is the same. . . .

The above is the content of C# Learning Diary 29----two-dimensional indexer and foreach traversal indexer. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

How to change the format of xml How to change the format of xml Apr 03, 2025 am 08:42 AM

There are several ways to modify XML formats: manually editing with a text editor such as Notepad; automatically formatting with online or desktop XML formatting tools such as XMLbeautifier; define conversion rules using XML conversion tools such as XSLT; or parse and operate using programming languages ​​such as Python. Be careful when modifying and back up the original files.

See all articles