简体   繁体   English

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

[英]How to downgrade this piece of code from .net 4.0 to 3.5?

I've been really loving this extension method in my .NET 4.0 code: 我一直很喜欢.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);
}

Now, I'd really like to use it in my .net 3.5 project, but it's missing the Contains method. 现在,我真的很想在我的.net 3.5项目中使用它,但是它缺少Contains方法。 How can I cleanly downgrade this extension method without complicating things too much? 我如何干净地降级此扩展方法而又不使事情复杂化?

Contains is an extension on IEnumerable introduced in 3.5 as part of LINQ. Contains是LINQ在3.5中引入的IEnumerable的扩展。 This code will compile under 3.5. 此代码将在3.5以下编译。

If it's not then make sure you have included 如果不是,请确保您已包括

using System.Linq

I agree with James Gaunt, this should run under 3.5 as is. 我同意James Gaunt的观点,这应该在3.5以下。

Perhaps you have neglected to add the using System.Linq; 也许您忽略了using System.Linq;添加using System.Linq; and using System.Collections.Generic; using System.Collections.Generic; declarations at the top of your code? 代码顶部的声明?

I get caught by that all the time. 我一直都被那个抓住。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM