迭代器
就是用来遍历的
引例:自己做一个List叫MyList,实现一个迭代器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Text5
{
class Program
{
class MyList
{
private int[] Nums { get; set; }
public MyList(int n)
{
var r = new Random();
Nums = new int[n];
for (var i = 0; i < n; i++)
{
Nums[i] = r.Next(0, 10);
}
}
private int Index=-1;
public bool MoveNext()
{
Index++;
return Index < Nums.Length;
}
public int Current { get { return Nums[Index]; } }
}
static void Main(string[] args)
{
MyList List = new MyList(5);
while (List.MoveNext())
{
Console.WriteLine(List.Current);
}
Console.ReadLine();
}
}
}
现在要求打印两次元素,加一个要求例如生成的三个数为1,2,3
想要输出:
1
——1
——2
——3
2
——1
——2
——3
3
——1
——2
——3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Text5
{
class Program
{
class Enumerator//定义一个迭代器
{
private int[] Nums=null;
public Enumerator(int[] nums)
{
this.Nums = nums;
}
private int Index = -1;
public bool MoveNext()
{
Index++;
return Index < Nums.Length;
}
public int Current { get { return Nums[Index]; } }
}
class MyList
{
private int[] Nums { get; set; }
public MyList(int n)
{
var r = new Random();
Nums = new int[n];
for (var i = 0; i < n; i++)
{
Nums[i] = r.Next(0, 10);
}
}
public Enumerator GetEnumerator()
{
return new Enumerator(Nums) ;
}
//private int Index=-1;
//public bool MoveNext()
//{
// Index++;
// return Index < Nums.Length;
//}
//public int Current { get { return Nums[Index]; } }
}
static void Main(string[] args)
{
MyList List = new MyList(5);
Enumerator e1 = List.GetEnumerator();
while (e1.MoveNext())
{
Console.WriteLine(e1.Current);
Enumerator e2 = List.GetEnumerator();
while (e2.MoveNext())
{
Console.WriteLine("---" + e2.Current);
}
}
Console.ReadLine();
}
}
}
C#已经将迭代器模式已经进行进一步封装
集合
集合就是实现了IEnumerable的接口
下面我们来实现IEnumerable接口的自定义的集合
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Text5
{
class Program
{
//class Enumerator//定义一个迭代器
//{
// private int[] Nums=null;
// public Enumerator(int[] nums)
// {
// this.Nums = nums;
// }
// private int Index = -1;
// public bool MoveNext()
// {
// Index++;
// return Index < Nums.Length;
// }
// public int Current { get { return Nums[Index]; } }
//}
class MyList:IEnumerable <int>
{
private int[] Nums { get; set; }
public MyList(int n)
{
var r = new Random();
Nums = new int[n];
for (var i = 0; i < n; i++)
{
Nums[i] = r.Next(0, 10);
}
}
public IEnumerator<int> GetEnumerator()
{
foreach (var item in Nums )
{
yield return item;
}
//throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
//throw new NotImplementedException();
}
//public Enumerator GetEnumerator()
//{
// return new Enumerator(Nums) ;
//}
//private int Index=-1;
//public bool MoveNext()
//{
// Index++;
// return Index < Nums.Length;
//}
//public int Current { get { return Nums[Index]; } }
}
static void Main(string[] args)
{
MyList List = new MyList(5);
foreach (var i1 in List)
{
Console.WriteLine(i1);
foreach (var i2 in List)
{
Console.WriteLine("---" + i2);
}
}
//var e1 = List.GetEnumerator();
//while (e1.MoveNext())
//{
// Console.WriteLine(e1.Current);
// var e2 = List.GetEnumerator();
// while (e2.MoveNext())
// {
// Console.WriteLine("---" + e2.Current);
// }
//}
Console.ReadLine();
}
}
}
foreach 实现里的接口
常用集合
List<>
Dictionary<>
Strack<>
Queue<>
LinkedList<>
static void Main(string[] args)
{
var dic = new Dictionary<int, string>();
dic.Add(1, "张三");
dic.Add(2, "李四");
dic.Add(3, "张三");
dic.Add(4, "张三");
foreach (var item in dic)
{
Console.WriteLine( item.Key.ToString());
Console.WriteLine( item.Value);
}
Console.ReadLine();
}