C#HashSet2工作完全一样的标准C#HashSet的,而不是编译 [英] C# HashSet2 to work exactly like the standard C# HashSet, not compiling

查看:209
本文介绍了C#HashSet2工作完全一样的标准C#HashSet的,而不是编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造我自己的HashSet的作品为标准的HashSet,使用字典。我这样做,因为C#的XNA的Xbox不支持HashSets。

I'm creating my own HashSet that works as the standard HashSet, using a Dictionary. I'm doing this because C# for XNA XBox doesn't support HashSets.

此代码是基于一个例子,我发现代码。 。我编辑的例子来解决一些问题,但它仍然不会编译

This code is based on code from an example I found. I've edited the example to fix some of the problems but it still won't compile.

public class HashSet2<T> : ICollection<T>
{
    private Dictionary<T, Int16> dict;

    // code has been edited out of this example
    // see further on in the question for the full class

    public IEnumerator<T> GetEnumerator()
    {
        throw new NotImplementedException();
    }

    IEnumerator<T> IEnumerable<T>.GetEnumerator()
    {
        return dict.GetEnumerator();
    }
}



.

'HashSet2<T>' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()'.
'HashSet2<T>.GetEnumerator()' cannot implement
'System.Collections.IEnumerable.GetEnumerator()'
because it does not have the matching return type of
'System.Collections.IEnumerator'

我也为信息感激上修复它更像如果它偏离标准的HashSet在它的行为或implments的方式,可能会有意想不到的是什么

I'd also be grateful for information on fixing it to be more like the standard HashSet if it deviates in it's behaviour or what it implments in ways that could be unexpected.

从续:的stackoverflow.com/questions/9966336/c-sharp-xna-xbox-hashset-and-tuple

最近之类的vertion:

The most recent vertion of the class:

public class HashSet2<T> : ICollection<T>
{
    private Dictionary<T, Int16> dict;
    // Dictionary<T, bool>

    public HashSet2()
    {
        dict = new Dictionary<T, short>();
    }

    public HashSet2(HashSet2<T> from)
    {
        dict = new Dictionary<T, short>();
        foreach (T n in from)
            dict.Add(n, 0);
    }

    public void Add(T item)
    {
        // The key of the dictionary is used but not the value.
        dict.Add(item, 0);
    }

    public void Clear()
    {
        dict.Clear();
    }

    public bool Contains(T item)
    {
        return dict.ContainsKey(item);
    }

    public void CopyTo(
        T[] array,
        int arrayIndex)
    {
        throw new NotImplementedException();
    }

    public bool Remove(T item)
    {
        return dict.Remove(item);
    }

    public System.Collections.IEnumerator GetEnumerator()
    {
        return ((System.Collections.IEnumerable)
            dict.Keys).GetEnumerator();
    }

    IEnumerator<T> IEnumerable<T>.GetEnumerator()
    {
        return ((IEnumerable<T>)
            dict.Keys).GetEnumerator();
    }

    public int Count
    {
        get {return dict.Keys.Count;}
    }

    public bool IsReadOnly
    {
        get {return false;}
    }
}


推荐答案

您想列举的钥匙,没有字典。试试这个:

You want to enumerate the keys, not the dictionary. Try this:

public IEnumerator GetEnumerator()
{
    return ((IEnumerable)dict.Keys).GetEnumerator();
}

IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
    return ((IEnumerable<T>)dict.Keys).GetEnumerator();
}

这篇关于C#HashSet2工作完全一样的标准C#HashSet的,而不是编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆