如果你没有C#3.0,那么你没有推广方法。
如果你没有的话。 NET3.5 那么,你没有将林克延伸方法称作静态。
您可以补充一下你自己的功能:
public static IEnumerable<T> Distinct(IEnumerable<T> src, IEqualityComparer<T> eCmp)
{
Dictionary<T, bool> fakeHashSet = new Dictionary<T, bool>(eCmp);
//When I coded for 2.0 I had my own custom HashSet<T>, but that s overkill here
bool dummy;
foreach(T item in src)
{
if(!fakeHashSet.TryGetValue(item, out dummy))
{
fakeHashSet.Add(item, true);
yield return item;
}
}
}
public static IEnumerable<T> Distinct(IEnumerable<T> src)
{
return Distinct(src, EqualityComparer<T>.Default);
}
public delegate TResult Func<T, TResult>(T arg);//we don t even have this :(
public static int Count(IEnumerable<T> src, Func<T, bool> predicate)
{
int c = 0;
foreach(T item in src)
if(predicate(item))
++c;
return c;
}
由于我们没有延期大会,或者说我们不得不这样说:
foreach (string value in Distinct(list))
{
System.Diagnostics.Debug.WriteLine(""{0}" occurs {1} time(s).", value, Count(list, delegate(string v){return v == value;}));
}
总而言之,我们能够用C#2.0来执行大部分林克到目标,我们当中许多人这样做了,但距离友好程度一样远,当然我们可以向其他问答者提供地图。
In this case though, you d be faster just doing the count directly:
Dictonary<string, int> counts = new Dictionary<string, int>();
foreach(string value in list)
{
if(counts.ContainsKey(value))
counts[value]++;
else
counts[value] = 1;
}
foreach(KeyValuePair<string, int> kvp in counts)
System.Diagnostics.Debug.WriteLine(""{0}" occurs {1} time(s).", kvp.Key, kvp.Value));