LINQ、泛型和对象初始化器能否在 .NET 2.0 Framework 中工作?
void Main()
{
List<Person> person = new List<Person>
{
new Person { Name = "Maria Anders", Age = 21 },
new Person { Name = "Ana Trujillo", Age = 55 },
new Person { Name = "Thomas Hardy", Age = 40 },
new Person { Name = "Laurence Lebihan", Age = 18 },
new Person { Name = "Victoria Ashworth", Age = 16 },
new Person { Name = "Ann Devon", Age = 12 }
};
person.Select(x => new { x.Name, x.Age }).Dump();
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
如果没有的话,能否将其转换为2.0编码。
void Main()
{
List<Person> person = new List<Person>
{
new Person { Name = "Maria Anders", Age = 21 },
new Person { Name = "Ana Trujillo", Age = 55 },
new Person { Name = "Thomas Hardy", Age = 40 },
new Person { Name = "Laurence Lebihan", Age = 18 },
new Person { Name = "Victoria Ashworth", Age = 16 },
new Person { Name = "Ann Devon", Age = 12 }
};
person.Select(x => new { x.Name, x.Age }).Dump();
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
If not, can you please convert it into 2.0 coding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 John 在评论中所说,这里有两个问题:C# 语言版本和 .NET 版本。
泛型适用于 C# 2 及更高版本(VS2005 及更高版本)以及 .NET 2.0 及更高版本。
对象初始化程序适用于 C# 3 及更高版本(VS2008 及更高版本),但不需要任何特定版本的 .NET Framework。
LINQ 需要 C# 3 或更高版本、和 .NET 3.5 及更高版本。
As John says in his comment, there are two issues here: C# language version and .NET version.
Generics will work in C# 2 and above (VS2005 and above), and .NET 2.0 and above.
Object initialisers will work in C# 3 and above (VS2008 and above), but do not require any particular version of the .NET Framework.
LINQ requires C# 3 or above, and .NET 3.5 and above.
根据您的 Dump 方法的用途,面向 .NET 2.0 的 C# 3.0 代码 * 可能如下所示:
* 请参阅 itowlson 的回答,了解有关 C# 版本与 .NET 平台版本的详细信息。
Depending what your Dump method is supposed to do, C# 3.0 code targeting .NET 2.0 * might look like this:
* See itowlson's answer for the details on C# version versus .NET platform version.