public static int CountImpl<T>(ICollection<T> collection)
{
return collection.Count;
}
public static int CountImpl<T>(ICollection collection)
{
return collection.Count;
}
public static int CountImpl<T>(string collection)
{
return collection.Length;
}
public static int CountImpl<T>(IEnumerable collection)
{
int count = 0;
foreach (object item in collection)
{
count++;
}
return count;
}
public static void PrintCount(IEnumerable collection)
{
dynamic d = collection;
int count = CountImpl<dynamic>(d);
Console.WriteLine(count);
}
调用
PrintCount(new BitArray(5));
PrintCount(new HashSet<int> { 1, 2 });
PrintCount("ABC");
PrintCount("ABCDEF".Where(c => c > 'B'));//返回大于'B'的字符
实例来至于《深入理解C#》(第三版)