扩展方法在 silverlight Windows Phone 7 中不起作用
我为 IEnumerable 编写了一种扩展方法 FindAll 来根据谓词条件进行过滤。数组和列表共有的。因为在 sil 中
代码:
public static IEnumerable<T> FindAll<T>(this IEnumerable<T> tCollection, Func<T, bool> predicate)
{
if (null == tCollection) yield return default(T);
using (IEnumerator<T> iterator = tCollection.GetEnumerator())
{
if (!iterator.MoveNext())
{
yield return default(T);
yield break;
}
do
{
if (predicate(iterator.Current))
yield return iterator.Current;
}
while (iterator.MoveNext());
}
}
当我打电话时它对我不起作用。
List<string> strings = new List<string> { "Kumaran", "Raj", "Kannan", "Karthi", "Gopal" };
IEnumerable<string> str = strings.FindAll(item => item.StartsWith("K"));
我能知道为什么它在我的应用程序中对我不起作用吗?
I wrote one extension method FindAll for IEnumerable to filter based on the predicate condition. for common to both Array and List. because In sil
the code:
public static IEnumerable<T> FindAll<T>(this IEnumerable<T> tCollection, Func<T, bool> predicate)
{
if (null == tCollection) yield return default(T);
using (IEnumerator<T> iterator = tCollection.GetEnumerator())
{
if (!iterator.MoveNext())
{
yield return default(T);
yield break;
}
do
{
if (predicate(iterator.Current))
yield return iterator.Current;
}
while (iterator.MoveNext());
}
}
it does not work for me when I am calling..
List<string> strings = new List<string> { "Kumaran", "Raj", "Kannan", "Karthi", "Gopal" };
IEnumerable<string> str = strings.FindAll(item => item.StartsWith("K"));
Can I know why it is not working for me in my application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我的猜测(尽管您可能没有向我们展示所有实际的相关代码)。
您将断点放置在 FindAll 方法中的某个位置。然后你运行该行:-
但你的断点没有被击中,对吧?
然而这是正确的。在第一次调用从
str
中的IEnumerable
获取的枚举器的MoveNext
之前,您的 FindAll 方法实际上不会被调用。因此,如果您在代码后面加上str
上的foreach
,您应该会看到代码开始发生。顺便说一句,您的实现看起来有点可疑,您肯定不希望在源为 null 或空时返回包含单个默认值的可枚举吗?
事实上,如果您确实想从
IEnumerable
返回过滤集,为什么不直接使用 LinqWhere
扩展方法呢。另一方面,如果您尝试创建在完整 .NET API 中找到的 FindAll 实现,那么您也应该返回List
。我怀疑你真正应该做的是将这个
using
添加到你的代码文件中:-然后你可以使用:-
如果你真的需要返回一个
FindAll
功能列出
,然后:-Here is my guess (although it could be that you haven't shown us all your actual relevant code).
You place break point somewhere in your FindAll method. Then you run the line:-
but your break point isn't hit, right?
However this is correct. Your FindAll method will not actually be invoked until the first call to
MoveNext
of an enumerator aquired from theIEnumerable
instr
. So if you followed your code with say aforeach
onstr
you should see code start to happen.BTW, your implementation looks a little suspect, surely you would not want to an enumerable containing single default value to be returned when the source is null or empty?
In fact if you really want to return a filtered set from an
IEnumerable<T>
why don't you just use the LinqWhere
extension method instead. On the other hand if you are trying to create an implementation of FindAll as found in the full .NET API for a List then you ought to be returning aList<T>
as well.I suspect that what you really should be doing is adding this
using
to your code file:-then you can use:-
and if you really need the
FindAll
feature that returns aList<T>
then:-