C#中假如一个自定义类数组可以使用foreach,那么要实现GetEnumerator()方法,下例为何没实现

.Net技术 码拜 8年前 (2016-03-05) 947次浏览
/*C#中通过实现IComparable接口对对象数组进行排序*/
using System;
class Student:IComparable
{
private string _name;
private int _age;
public Student(string name,int age)
{
_name = name;
_age = age;
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Age
{
get { return _age; }
set { _age = value; }
}
public int CompareTo(object right)
{
if(!(right is Student))
{
throw new ArgumentException(“参数必须为Student类型”);
}
return _name.CompareTo(((Student)right)._name);
}
public override string ToString()
{
return _name;
}
}
class Test
{
static void Main()
{
Student[] arr = new Student[5];
arr[0] = new Student(“张三”,5);
arr[1] = new Student(“李四”,3);
arr[2] = new Student(“王五”,2);
arr[3] = new Student(“马六”,4);
arr[4] = new Student(“钱七”,1);
Array.Sort(arr);
foreach(Student i in arr)    //这里的arr没有实现GetEnumerator()方法啊?
{
Console.WriteLine(i + ” ” + i.Age);
}
}
}
解决方案

5

C# 全部的数组都继承自Array,而Array实现了IEnumerable接口

5

namespace System
{
// 摘要:
//     提供创建、操作、搜索和排序数组的方法,因而在公共语言运行时中用作全部数组的基类。
[Serializable]
[ComVisible(true)]
public abstract class Array : ICloneable , IList , ICollection , IEnumerable , IStructuralComparable , IStructuralEquatable

5

            var arr = new int[16];
var basetype = arr.GetType( ).BaseType;
你可以本人测试,basetype即System.Array

15

引用 4 楼 twlyy 的回复:
Quote: 引用 1 楼 songbing774933 的回复:

C# 全部的数组都继承自Array,而Array实现了IEnumerable接口

namespace IEnumeratorSample
{
class Person : IEnumerable
{
public string Name;// 定义Person的名字
public string Age;// 定义Person的年龄
public Person(string name, string age)// 为Person初始化(构造函数)
{
Name = name;// 配置Name值
Age = age;
}
private Person[] per; //Person[] 说明这是个person类集合类型的域,它有一组元素,每个元素都是Person类的。
//per是这个域的名字,这是个 类型为person类数组的域,名字是per
public Person(Person[] array)// 重载构造函数,迭代对象
{
per = new Person[array.Length];// 创建对象
for (int i = 0; i < array.Length; i++)//遍历初始化对象
{
per[i] = array[i];// 数组赋值
}
}
public IEnumerator GetEnumerator()//实现接口
{
return this.per.GetEnumerator();//per是数组类型,已经实现了GetEnumerator()方法
}
}
class Program
{
static void Main(string[] args)
{
Person[] per = new Person[2]
{
new Person(“guojing”,”21″),
new Person(“muqing”,”21″),
};
Person personlist = new Person(per);
foreach (Person p in personlist)//遍历对象
{
Console.WriteLine(“Name is ” + p.Name + ” and Age is ” + p.Age);
}
Console.ReadKey();
}
}
}
这个例子就必须实现GetEnumerator()方法,怎么解释呢?

Person personlist = new Person(per);
和Student[] arr = new Student[5];  是一样的东西?
一个是单个的对象,一个是对象的数组
先好好理解类型吧

10

引用 5 楼 twlyy 的回复:
Quote: 引用 2 楼 songbing774933 的回复:

namespace System
{
// 摘要:
//     提供创建、操作、搜索和排序数组的方法,因而在公共语言运行时中用作全部数组的基类。
[Serializable]
[ComVisible(true)]
public abstract class Array : ICloneable , IList , ICollection , IEnumerable , IStructuralComparable , IStructuralEquatable

这里的arr是自定义的类数组,没有实现IEnumerable接口吧,

C#全部的数组,包括你所谓的自定义的类数组,都是继承自Array
你可以随便写个类
然后测试如下代码:
var arr = new YourClass[16];
var basetype = arr.GetType( ).BaseType;
你再看看basetype 是什么


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#中假如一个自定义类数组可以使用foreach,那么要实现GetEnumerator()方法,下例为何没实现
喜欢 (0)
[1034331897@qq.com]
分享 (0)