c#2.0 List 使用

本文通过实例演示了如何使用C#中的列表(List)进行数据处理,包括创建Person对象列表、遍历列表、按条件筛选列表项及对列表进行排序。
 

It is a fairly common programming scenario to find ourselves with a list of identical objects. In the past, without adequate support from programming languages, we found ourselves writing a lot of searching and sorting code, and that may have put you off using lists in favour of arrays. All that has changed with C# (particularly 2.0) - its implementation of a list makes handling such lists remarkably easy.

For example, given the following class Person:

public class Person

{

          public int age;

          public string name;

          public Person(int age, string name)

          {

                   this.age = age;

                   this.name = name;

          }

}

 

We can create a list of Person objects and add six people like so:

List<person>people =

C#'s list mechanism provides us with a number of useful methods. Personally, I find ForEach, FindAll and Sort to be very useful. ForEach allows us access to each item in the list. FindAll allows us to search for objects in the list that match a specific condition. Sort allows us to sort the objects in the list. The following code demonstrates how we might use each of these methods:

Console.WriteLine("Unsorted list");

 

people.ForEach(

delegate (Person p)
   { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

 

// Find the young

List<person> young = people.FindAll( delegate (Person p) { return p.age < 25; });
Console.WriteLine("Age
is less than 25");

young.ForEach(
delegate (Person p)
   { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

 

 

// Sort by name

Console.WriteLine("Sorted list, by name");
people.Sort(
delegate (Person p1, Person p2)
   {
return p1.name.CompareTo(p2.name); });

 

people.ForEach(

delegate (Person p)
   { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

 

 

// Sort by age

Console.WriteLine("Sorted list, by age");

 

people.Sort(

delegate (Person p1, Person p2)
   {
return p1.age.CompareTo(p2.age); });

 

people.ForEach(

delegate (Person p)
   { Console.WriteLine(String.Format("{0} {1}", p.age, p.name)); });

And here is the output that we should expect:

Unsorted list
50 Fred
30 John
26 Andrew
24 Xavier
5 Mark
6 Cameron

Age is less than 25
24 Xavier
5 Mark
6 Cameron

Sorted list, by name
26 Andrew
6 Cameron
50 Fred
30 John
5 Mark
24 Xavier

Sorted list, by age
5 Mark
6 Cameron
24 Xavier
26 Andrew
30 John
50 Fred

Lists are powerful and result in fewer, and more elegant, lines of code. Hopefully this short example has demonstrated their ease and you will find yourself using them in your day-to-day development activities.

new List<person>();

 

people.Add(

new Person(50, "Fred"));
people.Add(
new Person(30, "John"));
people.Add(
new Person(26, "Andrew"));
people.Add(
new Person(24, "Xavier"));
people.Add(
new Person(5, "Mark"));
people.Add(
new Person(6, "Cameron"));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值