你将如何实现IEnumerator接口?你将、如何实现、接口、IEnumerator

2023-09-02 21:45:38 作者:︶逗逼范儿ぴ

我有一个映射对象对象类,但不像词典它们映射两种方式。我现在想实现一个自定义IEnumerator接口,通过数值迭代。

 公共类映射< K,T> :IEnumerable的< T&GT ;,的IEnumerator< T>

{
    C5.TreeDictionary< K,T> KToTMap =新TreeDictionary< K,T>();
    C5.HashDictionary< T,K> TToKMap =新HashDictionary< T,K>();

    公共无效添加(K键,T值)
    {
        KToTMap.Add(键,值);
        TToKMap.Add(值,键);

    }

    公众诠释计数
    {
        {返回KToTMap.Count; }
    }


    公共ķ这个[T OBJ]
    {
        得到
        {
            返回TToKMap [OBJ]
        }
    }

    公共牛逼这个[K OBJ]
    {
        得到
        {
            返回KToTMap [OBJ]
        }
    }

    公众的IEnumerator< T>的GetEnumerator()
    {
        返回KToTMap.Values​​.GetEnumerator();
    }

    大众T电流
    {
        获得{抛出新的NotImplementedException(); }
    }

    公共无效的Dispose()
    {
        抛出新的NotImplementedException();
    }

    对象System.Collections.IEnumerator.Current
    {
        获得{抛出新的NotImplementedException(); }
    }

    公共BOOL的MoveNext()
    {
        ;
    }

    公共无效复位()
    {
        抛出新的NotImplementedException();
    }
}
 

解决方案

首先,不要让你的集合对象实现IEnumerator<>。这将导致错误。 (考虑情况下两个线程遍历相同的集合)。

实施枚举正确真可谓是不平凡的,所以C#2.0中加入特殊的语言支持这样做,立足于收益回报的语句。

Raymond Chen的近期的一系列博客文章(在C#中的迭代器及其后果的实施)是个好地方起床速度。

第1部分:http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx 第2部分:http://blogs.msdn.com/oldnewthing/archive/2008/08/13/8854601.aspx 第3部分:http://blogs.msdn.com/oldnewthing/archive/2008/08/14/8862242.aspx 第4部分:http://blogs.msdn.com/oldnewthing/archive/2008/08/15/8868267.aspx

I have a class that map objects to objects, but unlike dictionary it maps them both ways. I am now trying to implement a custom IEnumerator interface that iterates through the values.

public class Mapper<K,T> : IEnumerable<T>, IEnumerator<T>

{
    C5.TreeDictionary<K,T> KToTMap = new TreeDictionary<K,T>();
    C5.HashDictionary<T,K> TToKMap = new HashDictionary<T,K>();

    public void Add(K key, T value)
    {
        KToTMap.Add(key, value);
        TToKMap.Add(value, key);

    }

    public int Count
    {
        get { return KToTMap.Count; }
    }


    public K this[T obj]
    {
        get
        {
            return TToKMap[obj];
        }
    }

    public T this[K obj]
    {
        get
        {
            return KToTMap[obj];
        }
    }

    public IEnumerator<T> GetEnumerator()
    {
        return KToTMap.Values.GetEnumerator();
    }

    public T Current
    {
        get { throw new NotImplementedException(); }
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    object System.Collections.IEnumerator.Current
    {
        get { throw new NotImplementedException(); }
    }

    public bool MoveNext()
    {
        ;
    }

    public void Reset()
    {
        throw new NotImplementedException();
    }
}
C 中IDispose接口的实现方法以及为什么这么实现

解决方案

First, don't make your collection object implement IEnumerator<>. This leads to bugs. (Consider the situation where two threads are iterating over the same collection).

Implementing an enumerator correctly turns out to be non-trivial, so C# 2.0 added special language support for doing it, based on the 'yield return' statement.

Raymond Chen's recent series of blog posts ("The implementation of iterators in C# and its consequences") is a good place to get up to speed.

Part 1: http://blogs.msdn.com/oldnewthing/archive/2008/08/12/8849519.aspx Part 2: http://blogs.msdn.com/oldnewthing/archive/2008/08/13/8854601.aspx Part 3: http://blogs.msdn.com/oldnewthing/archive/2008/08/14/8862242.aspx Part 4: http://blogs.msdn.com/oldnewthing/archive/2008/08/15/8868267.aspx