索引器与迭代器的使用

选代器的本地文件 E:/diablo/Inumerator/ClassLibrary2/ClassLibrary2/Program.cs

C#索引器使用方法介绍
访问类中的集合,希望好像类本身就是一个数组一样引器是一种c#的语法构造,可以用我们熟悉的数组方括号语法访问类中的集合
语法:类型 this[类型 参数] {get;set;}

public class ListBoxTest
...{
private string[] strings;
private int ctr = 0 ;
public ListBoxTest(params string[] initialStrings)
...{
strings = new string[256];
foreach(string s in initialStrings)
...{
strings[ctr++] = s;
}
}
public void Add(string theString)
...{
if(ctr>=strings.Length)
...{
//错误处理
}
else
strings[ctr++] = theString;
}

允许数组式访问#region 允许数组式访问
public string this[int index]
...{
get
...{
if(index<0 || index>=strings.Length)
...{
//错误处理
}
return strings[index];
}
set
...{
if(index>=ctr)
...{
//错误处理
}
else
strings[index] = value;
}

}
#endregion

索引string#region 索引string
private int findString(string searchString)
...{
for(int i = 0;i<strings.Length;i++)
...{
if(strings==searchString)
return i;
}
return -1;
}
public string this[string index]
...{
get
...{
if(index.Length==0)
...{
//错误处理
}
return this[findString(index)];
}
set
...{
strings[findString(index)] = value;
}
}
#endregion

public int GetNumArray()
...{
return ctr;
}
}
static void Main(string[] args)
...{

ListBoxTest listBox = new ListBoxTest("Hello","world");
listBox.Add("who");
listBox.Add("are");
listBox.Add("you");
string subset = "Universe";
listBox[1] = subset;

for (int i = 0; i < listBox.GetNumArray(); i++)
...{
Console.WriteLine(listBox);
}

Console.WriteLine(listBox["who"]);
}

结果为:

hello

universe

who

are

you

who
 

没有迭代器的时候,创建一个可用于foreach的集合(c# 1.0):

 public class mycollection : ienumerable
 {
  public myenumerator getenumerator()
  {
   return new myenumerator(this);
  }
  public class myenumerator : ienumerator
  {
   public void reset(){...}
   public bool movenext(){...}
   public int current{ get{...} }
   object ienumerator.current{ get{...} }
  }
 }

对集合使用foreach语句:
  foreach(int i in col){...}

 相单于:
 ienumerator etor = ((ienumerable)col).getenumerator();
 try
 {
  while(etor.movenext())
  {
   elementtype clem (elementtype)etor.current;
   ...;
  }
 }
 finally{(idisposable)enumerator).dispose();}

c# 2.0 中的迭代器

使用迭代器创建于foreach的集合(c# 2.0):

 public class stack<t>:ienumerable<t>
 {
  t[] items;
  int count;
  public void push(t data){...}
  public t pop(){...}
  public ienumerator<t> getenumerator()
  {
   for(int i=count-1;i>=0;--i)
   {
     yield return items[i];
   }
  }
 }

 使用foreach语句:

 stack<int> stack = new stack<int>();
 foreach(int i in statck){...}

 使用迭代器创建倒序遍历:

 public ienumerable<t> bottomtotop
 {
    get
    {
       for(int i=0;i<count;i++)
       {
          yield return items[i];
       }
    }
 }

迭代器中的yield语句

使用yield return 产生枚举元素:

 for(int i=count-1;i>=0;--i)
 {
    yield return items[i];
 }

使用yield break中断迭代:
 
 for(int i=count-1;i>=0;--i)
 {
    yield return items[i];
    if(items[i]>10)
        yield break;
 }

迭代器的机制

c# 2.0中的迭代器同样是通过编译器的一层额外处理,来简化创建可用于foreach的枚举集合的工作。

通过ildasm.exe反汇编工作,我们能够获得对迭代器的深入理解:
   - 迭代器中的getenumerator()方法
   - 迭代器中的嵌套类
   - 迭代器中的yield语句

总结
 
匿名方法允许我们以一种“内联”的方式将代码直接和委托实例相关联,从而使得委托实例化的工作更加直观和方便。迭代器允许我们更加方便地编写应用于foreach语句的枚举集合。

对于类似于c#这样的高级语言,掌控好他一个很重要的地方就是掌控编译器在背后为我们做了哪些工作。c# 2.0中的匿名方法和迭代器都是用国在编译层面进行一些额外的处理,进而来简化程式员的工作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值