using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace _28_foreach
{
class Program
{
static void Main(string[] args)
{
ArrayList array = new ArrayList() { 1, 2, 3, 4, 5 };
foreach (object item in array)
{
Console.WriteLine(item);
}
Console.WriteLine("===============================");
List<string> list = new List<string>() { "ABC", "DEF", "GHI" };
foreach (var item in list)
{
Console.WriteLine(item);
}
Console.WriteLine("===============================");
string[] names = { "C#", "VB", "VC" };
foreach (var item in names )
{
Console.WriteLine(item);
}
Console.WriteLine("===============================");
Person p = new Person();
p[0] = "奥迪";
p[1] = "奔驰";
p[2] = "宝马";
//for (int i = 0; i < p.Count; i++)
//{
// Console.WriteLine(p[i]);
//}
//任何类型想使用foreach遍历,就必须在当前类型中存在
//public IEnumerator GetEnumerator()方法
//一般通过实现接口来创建该方法
foreach (string item in p)
{
Console.WriteLine(item);
//内部实现的原理(跟使用foreach实现相同的作用)
IEnumerator etor = p.GetEnumerator();
while (etor.MoveNext())
{
string str = etor.Current.ToString();
Console.WriteLine(str);
}
Console.ReadKey();
}
}
public class Person:IEnumerable
{
private List<string> listcars = new List<string>();
//用于返回数量
public int Count
{
get
{
return this.listcars.Count;
}
}
public string this[int index]
{
get
{
return listcars[index];
}
set
{
if (index >= listcars.Count)
{
listcars.Add(value);
}
listcars[index] = value;
}
}
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public string Email
{
get;
set;
}
//获取一个对象,而这个对象是用来遍历的
public IEnumerator GetEnumerator()
{
return new PersonEnumerator(listcars);
}
}
public class PersonEnumerator : IEnumerator
{
//将person中car的集合传进来
public PersonEnumerator(List<string> _cars)
{
cars = _cars;
}
public List<string> cars;
//自己模拟当前对象的索引
private int index = -1;
//获取当前正在遍历的对象
public object Current
{
get
{
if (index < 0)
{
return null;
}
return cars[index];
}
}
public bool MoveNext()
{
index = index + 1;
if (index >= cars.Count)
{
return false;
}
else
{
return true;
}
}
public void Reset()
{
index = -1;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace _28_foreach
{
class Program
{
static void Main(string[] args)
{
ArrayList array = new ArrayList() { 1, 2, 3, 4, 5 };
foreach (object item in array)
{
Console.WriteLine(item);
}
Console.WriteLine("===============================");
List<string> list = new List<string>() { "ABC", "DEF", "GHI" };
foreach (var item in list)
{
Console.WriteLine(item);
}
Console.WriteLine("===============================");
string[] names = { "C#", "VB", "VC" };
foreach (var item in names )
{
Console.WriteLine(item);
}
Console.WriteLine("===============================");
Person p = new Person();
p[0] = "奥迪";
p[1] = "奔驰";
p[2] = "宝马";
//for (int i = 0; i < p.Count; i++)
//{
// Console.WriteLine(p[i]);
//}
//任何类型想使用foreach遍历,就必须在当前类型中存在
//public IEnumerator GetEnumerator()方法
//一般通过实现接口来创建该方法
foreach (string item in p)
{
Console.WriteLine(item);
}
Console.WriteLine("===============================");
//内部实现的原理(跟使用foreach实现相同的作用)
IEnumerator etor = p.GetEnumerator();
while (etor.MoveNext())
{
string str = etor.Current.ToString();
Console.WriteLine(str);
}
Console.ReadKey();
}
}
public class Person:IEnumerable
{
private List<string> listcars = new List<string>();
//用于返回数量
public int Count
{
get
{
return this.listcars.Count;
}
}
public string this[int index]
{
get
{
return listcars[index];
}
set
{
if (index >= listcars.Count)
{
listcars.Add(value);
}
listcars[index] = value;
}
}
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public string Email
{
get;
set;
}
//获取一个对象,而这个对象是用来遍历的
public IEnumerator GetEnumerator()
{
return new PersonEnumerator(listcars);
}
}
public class PersonEnumerator : IEnumerator
{
//将person中car的集合传进来
public PersonEnumerator(List<string> _cars)
{
cars = _cars;
}
public List<string> cars;
//自己模拟当前对象的索引
private int index = -1;
//获取当前正在遍历的对象
public object Current
{
get
{
if (index < 0)
{
return null;
}
return cars[index];
}
}
public bool MoveNext()
{
index = index + 1;
if (index >= cars.Count)
{
return false;
}
else
{
return true;
}
}
public void Reset()
{
index = -1;
}
}
}