If you do not have C # 3.0, then you do not have extension methods.
If you do not have .NET3.5, then you do not have any Linq extension methods to call as statics.
You can add your own for some of these features:
public static IEnumerable<T> Distinct(IEnumerable<T> src, IEqualityComparer<T> eCmp)
{
Dictionary<T, bool> fakeHashSet = new Dictionary<T, bool>(eCmp);
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);
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;
}
Since we don't have extension syntax or lamdbas, we should call them like this:
foreach (string value in Distinct(list))
{
System.Diagnostics.Debug.WriteLine("\"{0}\" occurs {1} time(s).", value, Count(list, delegate(string v){return v == value;}));
}
, Linq-to-objects # 2.0, , , , , .
, , :
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));