日期:2014-05-17  浏览次数:20855 次

帮忙补下扩展方法
希望写个扩展方法能被这样调用
string[] ss = { "1", "22", "333", "4444", "5" };
int ss1 = ss.Count((temp,index) => temp.Length >= index);
计算序列中元素长度大于索引的元素数量
返回值应该是4.

---以下就是参数和实体实现不知道怎么写了
public static class cc
  {
  public static int Count(this string[] val)
  {
  return 0;
  }
  }

主要想了解下扩展方法怎么写




------解决方案--------------------
private static int Count(this string[] arr)
{
int count = 0;
for (int index = 0; index < arr.Length; index++)
{
if (arr[index].Length >= index)
count++;
}
return count;
}
试试看这个可以么?
------解决方案--------------------
C# code
 
   public static class StringHelper
    {
        public static int Count(this string[] ary, Func<string, int, bool> fun)
        {
            return ary.Where((str, index) => { return fun(str, index); }).Count();
        }
    }