110,566
社区成员
发帖
与我相关
我的任务
分享
public static class MyStringExt
{
public static int Count(this MyString thiz, Func<char, bool> fun)
{
int count = 0;
foreach (var c in thiz)
{
if (fun(c)){ count++; }
}
return count;
}
}
public class MyString
{
private string _value;
public MyString(string value)
{
_value = value;
}
public IEnumerator<char> GetEnumerator()
{
if (_value == null) { throw new Exception("没有枚举集合"); }
foreach (var c in _value)
{
yield return c;
}
}
static void Main(string[] args)
{
MyString str =new MyString("hello");
Console.WriteLine(str.Count(a => a == 'l')) ;
}
}