List<T>
List<t>怎么遍历数组,求解
[解决办法]
foreach (var item in list)
{
}
[解决办法]
++ foreach
[解决办法]
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(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;
}
[解决办法]
foreach(类型(泛型中<类型>) item in List<类型>)
[解决办法]
终于看见一个会的了...