读书人

帮忙补上扩展方法

发布时间: 2012-11-20 09:55:43 作者: rapoo

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

读书人网 >C#

热点推荐