


C# Learning Diary 29----Two-dimensional indexer and foreach traversal indexer
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)+"号"); } } } } }
Result:
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); }
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); } } } }
The results are the same:
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)+"号"); } } } }
Result:
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)!

C# and .NET provide powerful features and an efficient development environment. 1) C# is a modern, object-oriented programming language that combines the power of C and the simplicity of Java. 2) The .NET framework is a platform for building and running applications, supporting multiple programming languages. 3) Classes and objects in C# are the core of object-oriented programming. Classes define data and behaviors, and objects are instances of classes. 4) The garbage collection mechanism of .NET automatically manages memory to simplify the work of developers. 5) C# and .NET provide powerful file operation functions, supporting synchronous and asynchronous programming. 6) Common errors can be solved through debugger, logging and exception handling. 7) Performance optimization and best practices include using StringBuild

.NETFramework is a cross-language, cross-platform development platform that provides a consistent programming model and a powerful runtime environment. 1) It consists of CLR and FCL, which manages memory and threads, and FCL provides pre-built functions. 2) Examples of usage include reading files and LINQ queries. 3) Common errors involve unhandled exceptions and memory leaks, and need to be resolved using debugging tools. 4) Performance optimization can be achieved through asynchronous programming and caching, and maintaining code readability and maintainability is the key.

Reasons for C#.NET to remain lasting attractive include its excellent performance, rich ecosystem, strong community support and cross-platform development capabilities. 1) Excellent performance and is suitable for enterprise-level application and game development; 2) The .NET framework provides a wide range of class libraries and tools to support a variety of development fields; 3) It has an active developer community and rich learning resources; 4) .NETCore realizes cross-platform development and expands application scenarios.

Design patterns in C#.NET include Singleton patterns and dependency injection. 1.Singleton mode ensures that there is only one instance of the class, which is suitable for scenarios where global access points are required, but attention should be paid to thread safety and abuse issues. 2. Dependency injection improves code flexibility and testability by injecting dependencies. It is often used for constructor injection, but it is necessary to avoid excessive use to increase complexity.

C#.NET is widely used in the modern world in the fields of game development, financial services, the Internet of Things and cloud computing. 1) In game development, use C# to program through the Unity engine. 2) In the field of financial services, C#.NET is used to develop high-performance trading systems and data analysis tools. 3) In terms of IoT and cloud computing, C#.NET provides support through Azure services to develop device control logic and data processing.

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
