一个集合接口的问题
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace collection_interface
{
public class listboxtest : IEnumerable<String>为什么这里没有声名ienumerator接口。该接口为什么叫实现式接口呢? {
private string[] strings;
private int ctr = 0;
public IEnumerator<string> GetEnumerator()
{
foreach (string s in strings)
{
yield return s;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public listboxtest(params string[] initialstrings)
{
strings = new String[8];
foreach (string s in initialstrings)
{
strings[ctr++] = s;
}
}
public void add(string thestring)
{
strings[ctr] = thestring;
ctr++;
}
public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
}
return strings[index];
}
set
{
strings[index] = value;
}
}
public int getnumentries()
{
return ctr;
}
}
class Program
{
static void Main(string[] args)
{
listboxtest lbt = new listboxtest("hello", "world");
lbt.add("who");
lbt.add("is");
lbt.add("douglas");
lbt.add("adams");
string subst = "universe";
lbt[1] = subst;
foreach (string s in lbt)
{
Console.WriteLine("value:{0}", s);
}
}
}
}
------解决方案--------------------如果你说的是现实yield return,那只是GetEnumerator函数返回的一个接口
------解决方案--------------------