索引器实现for循环
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
People people = new People();
people[0] = "a";
people[1] = "b";
for (int i = 0; i < people.Count; i++)
{
Console.WriteLine(people[i]);
}
Console.ReadKey();
}
}
public class People
{
Hashtable hash = new Hashtable();
public int Count
{
get { return hash.Keys.Count; }
}
//索引器
public string this[int index]
{
get { return hash[index].ToString(); }
set { hash.Add(index, value); }
}
}
}
枚举器实现foreach
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
People people = new People();
people[0] = "a";
people[1] = "b";
for (int i = 0; i < people.Count; i++)
{
Console.WriteLine(people[i]);
}
foreach (string p in people)
{
Console.WriteLine(p);
}
Console.ReadKey();
}
}
public class People : IEnumerable
{
Hashtable hash = new Hashtable();
public int Count
{
get { return hash.Keys.Count; }
}
//索引器
public string this[int index]
{
get { return hash[index].ToString(); }
set { hash.Add(index, value); }
}
// 类里有GetEnumerator()就能实现foreach,可以不继承 IEnumerable 接口
public IEnumerator GetEnumerator()
{
return new PeopleEnumerator(hash);//返回枚举器
}
其实有下面这个迭代器就可以了,用yield return,编译器会帮忙生成枚举器
//public IEnumerator GetEnumerator()
//{
// for (int i = 0; i < hash.Keys.Count; i++)
// {
// yield return hash[i].ToString();
// }
//}
}
//枚举器
public class PeopleEnumerator : IEnumerator
{
private Hashtable hash;
private int index = -1;
public PeopleEnumerator(Hashtable _hash)
{
hash = _hash;
}
public object Current
{
get
{
if (index > -1 || index < hash.Keys.Count)
{
return hash[index].ToString();
}
else
{
return new IndexOutOfRangeException();
}
}
}
public bool MoveNext()
{
if (index + 1 < hash.Keys.Count)
{
index++;
return true;
}
index = -1;
return false;
}
public void Reset()
{
index = -1;
}
}
}
** IEnumerator泛型接口;foreach中var能否类型推断的解释**
如图,将上面代码的foreach (string p in people)中的string改为var,推断var为object类型
这是枚举中代码
public object Current
{
get
{
if (index > -1 || index < hash.Keys.Count)
{
return hash[index].ToString();
}
else
{
return new IndexOutOfRangeException();
}
}
}
的返回是object
枚举器实现IEnumerator<T>接口就可以使var能推断出想要的类型了,主要就两点变化,见注释
代码如下
public class People/*:IEnumerable<string>*/
{
Hashtable hash = new Hashtable();
public int Count
{
get { return hash.Keys.Count; }
}
public string this[int index]
{
get { return hash[index].ToString(); }
set { hash.Add(index, value); }
}
// 这个类就不继承IEnumerator<string>接口了,GetEnumerator()方法直接返回IEnumerator<string>就OK,如果继承IEnumerator<string>,因为IEnumerator<string>还继承了IEnumerator,还得多写两个显示实现的方法
public IEnumerator<string> GetEnumerator()
{
return new PeopleEnumerator(hash);
}
//IEnumerator<string> IEnumerable<string>.GetEnumerator()
//{
// throw new NotImplementedException();
//}
//IEnumerator IEnumerable.GetEnumerator()
//{
// throw new NotImplementedException();
//}
}
//枚举器得继承泛型接口
public class PeopleEnumerator : IEnumerator<string>
{
private Hashtable hash;
private int index = -1;
public PeopleEnumerator(Hashtable _hash)
{
hash = _hash;
}
public string Current
{
get
{
return hash[index].ToString();
}
}
object IEnumerator.Current
{
get
{
return hash[index].ToString();
}
}
public void Dispose()
{
}
public bool MoveNext()
{
if (index + 1 < hash.Keys.Count)
{
index++;
return true;
}
index = -1;
return false;
}
public void Reset()
{
index = -1;
}
}
显示实现接口可以参考:
http://blog.csdn.net/m0_38110784/article/details/78040912
PS:HashTable的foreach里的var不能类型推断,Dictionary的可以就是这个原因
,所以HashTable一般 foreach (DictionaryEntry h in hash){},这样h才能 . 出Key。
而Dictionary 用 foreach(var d in dic ) ,d就能 . 出Key。
** IComparable<T> 比较器**
实现IComparable<T>接口的CompareTo方法。