日期:2014-05-20  浏览次数:20901 次

List<T>
List<t>怎么遍历数组,求解
------解决方案--------------------
 foreach (var item in list)
             { 
             }

------解决方案--------------------
引用:
C# code
 foreach (var item in list)
             { 
             }

++ foreach
------解决方案--------------------
foreach 或 for
都可以
------解决方案--------------------
引用:
foreach 或 for
都可以


++
------解决方案--------------------

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


List<Student> stuLists = new List<Student>();

stuLists.Add(new Student(){Name="Tom",Age=25});
stuLists.Add(new Student(){Name="Jerry",Age=27});

foreach(Student s in stuLists)
{
   //遍历输出
   Response.Write("姓名:"+s.Name+",年龄:"+s.Age+"\n");
}

------解决方案--------------------
引用:
foreach 或 for
都可以

可以百度查怎么用~!
------解决方案--------------------
foreach(var item in list)
{
Console.WriteLine(item);
  
}
------解决方案--------------------
foreach遍历,会将原集合置为ReadOnly,for则不会。
------解决方案--------------------
当然是用Linq咯:
有高级语言支持不用,简直就是悲剧。
List<T> stuLists = new List<Student>();

stuLists.Add(new Student(){Name="Tom",Age=25});
stuLists.Add(new Student(){Name="Jerry",Age=27});

stuLists.foreach(o=>Response.Write("姓名:"+o.Name+",年龄:"+o.Age+"\n"));
------解决方案--------------------
public class Student
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
            public Student(int id, string name, int age)
            {
                this.ID = id;
                this.Name = name;
                this.Age = age;
            }
        }

public string MyLinq()
        {
            string str_return = string.Empty;
            List<Student> Mylist = new List<Student>();
            Mylist.Add(new Student(1, "yh", 20));
            Mylist.Add(new Student(2, "yh", 20));
            Mylist.Add(new Student(3, "yh", 20));
            Mylist.Add(new Student(4, "yy", 20));
            Mylist.Add(new Student(1, "qb", 10));
            var d1 = from stu in Mylist where stu.Age == 20 select stu;
            var d2 = from stu in Mylist where stu.Name.Contains("qb") select stu;
            foreach (Student s in d2)
            {
                str_return += " " + string.Format("{0},{1},{2}", s.ID, s.Name, s.Age)+"    ";
            }
            return str_return;
        }


------解决方案--------------------