本文介绍了你将如何实现IEnumerator接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类映射对象到对象,但不像字典,它映射他们的方式。我现在正试图实现一个定制的IEnumerator接口,通过这些值进行迭代。

  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当前
{
get {throw new NotImplementedException(); }
}

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

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

public bool MoveNext()
{
;
}

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


解决方案

,不要使你的集合对象实现IEnumerator<>。这导致错误。 (考虑两个线程在同一个集合上迭代的情况)。



正确实现枚举器是非常重要的,因此C#2.0基于'yield return'语句增加了特殊语言支持。



Raymond Chen最近的一系列博客文章(C#中的迭代器及其后果的实现)是加快速度的好地方。




  • 第1部分:

  • 部分2:

  • 第3部分:

  • 第4部分:


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();
    }
}
解决方案

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.

这篇关于你将如何实现IEnumerator接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 08:18