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 Ashwort开发者_如何转开发h", 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.
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.
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.
精彩评论