列表< Of T>在.NET 3.0中有所区别?

NET 3.0。
List(Of T)
上有
Distinct
吗? 我需要导入哪些包? 如果没有,是否有等价?     
已邀请:
不,您必须自己动手,或将项目更改为.NET 3.5或4.0。     
在.NET 3.0中,一个选项是使用带有虚拟值的
Dictionary<,>
。例如。 (不处理
null
s):
List<Foo> foos  = ...
Dictionary<Foo, bool> dict = new Dictionary<Foo, bool>();

foreach(Foo foo in foos)
   dict[foo] = true;

ICollection<Foo> distinctFoos = dict.Keys;
如果你不喜欢这个'hack',你将不得不推出自己的set类。 编辑:这是一个处理来源中的
null
s的版本:
public static IEnumerable<T> Distinct<T>(IEnumerable<T> source)
{
    if (source == null)
        throw new ArgumentNullException("source");

    Dictionary<T, bool> dict = new Dictionary<T, bool>();
    bool nullSeen = false;

    foreach (T item in source)
    {
        if (item == null)
        {
            if (!nullSeen)
                yield return item;

            nullSeen = true;
        }

        else if (!dict.ContainsKey(item))
        {
            dict.Add(item, true);
            yield return item;
        }
    }
}
    
在.NET 3中,您必须自己动手。如果你看一下从列表中删除重复项&lt; T&gt;在C#中,Keith使用HashSet实现了Distinct for .NET 2。 这是System.Core程序集的一部分,因此您需要在项目中引用它,这意味着安装.NET 3.5框架,但是当.NET 3.5在相同版本的CLR上运行时,您将无法获得这样做的任何问题。部署时,您需要确保在客户端计算机上安装了.NET 3.5框架,或者在发布目录中包含System.Core.dll。     
LinqBridge将允许您以.Net 3.0为目标,但仍然使用Linq。这包括
IEnumerable(Of T)
上的
Distinct
扩展方法。     

要回复问题请先登录注册