不知何故,以下代码不会在 VS2010 中编译,而是在 VS2012 中编译而无需更改。VS2010 中的问题线是
names.Select(foo.GetName)
错误 CS1928:“string[]”不包含“Select”的定义和最佳扩展方法重载“System.Linq.Enumerable.Select<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System. Func<TSource,TResult>)' 有一些无效参数。
using System;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
var foo = new Foo();
var names = new[] {"Hello"};
Console.WriteLine(string.Join(", ", names.Select(foo.GetName)));
}
}
public class Foo
{
}
static class Extensions
{
public static string GetName(this Foo foo, string name)
{
return name;
}
}
}