日期:2014-05-16 浏览次数:20891 次
class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>();
students.Add(new Student() {Name="A",Sex="Man",Age="12" });
students.Add(new Student() { Name = "B", Sex = "Man", Age = "13" });
students.Add(new Student() { Name = "C", Sex = "Man", Age = "14" });
students.Add(new Student() { Name = "D", Sex = "Man", Age = "15" });
//一种访问方法,迭代访问
foreach (Student student in students)
Console.WriteLine("Name:"+student.Name+"Sex:"+student.Sex+"Age:"+student.Age+"\n\t");
//循环输出A B C D的属性
Console.ReadLine();
//一种是按所在List数组的索引序号访问
Console.WriteLine(students[2].Name);
//输出C
Console.ReadLine();
//一种是用linq查询访问
var s = from student in students
where student.Name == "B"
select student;
foreach(Student B in s)
Console.WriteLine("Name:"+B.Name+"Sex:"+B.Sex+"Age:"+B.Age);
//输出B ,Man,13
Console.ReadLine();
//一种是用Find函数
Student C = students.Find(x => x.Name == "C");