扩展方法在 silverlight Windows Phone 7 中不起作用

发布于 2024-10-09 16:15:34 字数 950 浏览 0 评论 0原文

我为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最笨的告白 2024-10-16 16:15:34

这是我的猜测(尽管您可能没有向我们展示所有实际的相关代码)。

您将断点放置在 FindAll 方法中的某个位置。然后你运行该行:-

IEnumerable<string> str = strings.FindAll(item => item.StartsWith("K"));

但你的断点没有被击中,对吧?

然而这是正确的。在第一次调用从 str 中的 IEnumerable 获取的枚举器的 MoveNext 之前,您的 FindAll 方法实际上不会被调用。因此,如果您在代码后面加上 str 上的 foreach,您应该会看到代码开始发生。

顺便说一句,您的实现看起来有点可疑,您肯定不希望在源为 null 或空时返回包含单个默认值的可枚举吗?

事实上,如果您确实想从 IEnumerable 返回过滤集,为什么不直接使用 Linq Where 扩展方法呢。另一方面,如果您尝试创建在完整 .NET API 中找到的 FindAll 实现,那么您也应该返回 List

我怀疑你真正应该做的是将这个 using 添加到你的代码文件中:-

using System.Linq;

然后你可以使用:-

IEnumerable<string> str = strings.Where(item => item.StartsWith("K"));

如果你真的需要返回一个 FindAll 功能列出,然后:-

List<string> str = strings.Where(item => item.StartsWith("K")).ToList();

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:-

IEnumerable<string> str = strings.FindAll(item => item.StartsWith("K"));

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 the IEnumerable in str. So if you followed your code with say a foreach on str 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 Linq Where 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 a List<T> as well.

I suspect that what you really should be doing is adding this using to your code file:-

using System.Linq;

then you can use:-

IEnumerable<string> str = strings.Where(item => item.StartsWith("K"));

and if you really need the FindAll feature that returns a List<T> then:-

List<string> str = strings.Where(item => item.StartsWith("K")).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文