首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

帮忙补上扩展方法

2012-11-20 
帮忙补下扩展方法希望写个扩展方法能被这样调用string[] ss { 1, 22, 333, 4444, 5 }int ss1

帮忙补下扩展方法
希望写个扩展方法能被这样调用
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();        }    } 

热点排行