LINQ、泛型和对象初始化器能否在 .NET 2.0 Framework 中工作?

发布于 2024-10-17 10:16:35 字数 624 浏览 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; }
}

如果没有的话,能否将其转换为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 技术交流群。

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

发布评论

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

评论(2

遮了一弯 2024-10-24 10:16:35

正如 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.

夏花。依旧 2024-10-24 10:16:35

根据您的 Dump 方法的用途,面向 .NET 2.0 的 C# 3.0 代码 * 可能如下所示:

static 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.ForEach(x => Dump(x));

}

static void Dump(Person p)
{
    Console.WriteLine("{0} {1}", p.Name, p.Age);
}

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

* 请参阅 itowlson 的回答,了解有关 C# 版本与 .NET 平台版本的详细信息。

Depending what your Dump method is supposed to do, C# 3.0 code targeting .NET 2.0 * might look like this:

static 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.ForEach(x => Dump(x));

}

static void Dump(Person p)
{
    Console.WriteLine("{0} {1}", p.Name, p.Age);
}

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

* See itowlson's answer for the details on C# version versus .NET platform version.

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