如何将这段代码从.net 4.0降级到3.5?

发布于 2024-10-02 04:42:50 字数 301 浏览 2 评论 0原文

我非常喜欢 .NET 4.0 代码中的这种扩展方法:

public static bool In<T>(this T source, params T[] list)
{
  if(null==source) throw new ArgumentNullException("source");
  return list.Contains(source);
}

现在,我真的很想在我的 .net 3.5 项目中使用它,但它缺少 Contains 方法。我怎样才能干净地降级这个扩展方法而不让事情变得太复杂?

I've been really loving this extension method in my .NET 4.0 code:

public static bool In<T>(this T source, params T[] list)
{
  if(null==source) throw new ArgumentNullException("source");
  return list.Contains(source);
}

Now, I'd really like to use it in my .net 3.5 project, but it's missing the Contains method. How can I cleanly downgrade this extension method without complicating things too much?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

可遇━不可求 2024-10-09 04:42:50

Contains 是 IEnumerable 的扩展,作为 LINQ 的一部分在 3.5 中引入。该代码将在 3.5 下编译。

如果不是,请确保您已包含

using System.Linq

Contains is an extension on IEnumerable introduced in 3.5 as part of LINQ. This code will compile under 3.5.

If it's not then make sure you have included

using System.Linq
温柔少女心 2024-10-09 04:42:50

我同意 James Gaunt 的观点,按原样运行应该低于 3.5。

也许您忽略了在代码顶部添加 using System.Linq;using System.Collections.Generic; 声明?

我总是被这个问题所困扰。

I agree with James Gaunt, this should run under 3.5 as is.

Perhaps you have neglected to add the using System.Linq; and using System.Collections.Generic; declarations at the top of your code?

I get caught by that all the time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文