C# List 泛型值的遍历

来源:百度知道 编辑:UC知道 时间:2024/04/29 07:37:59
public static List<user> getInfo()
{
List<user> list_u =new List<user>();
sql = "select * from users "
using (SqlDataReader reader = connDB.ExeReadsql(sql))
{
while (reader.Read())
{
user u = new user();
u.Id=int.Parse(reader["ID"].ToString());
u.Name= reader["Name"].ToString();
u.pwd= reader["password"].ToString();
list_u.Add(u);
}
}
return list_u;
}
之后 我如何来接收用户名和密码了? 如何遍历List<>中的值了?

谢谢

foreach(user u in list_u)
{
u.name
u.password
}

这是的List<user> list_u =new List<user>();
集合单位是个user对象
用foreach循环好理解
用for也行
for(int i = 0;i<list_u.lenght;i++){
user u = list_u[i];
Console.WriteLine("用户名是:"+u.Name);
Console.WriteLine("编号是:"+u.Id);
Console.WriteLine("密码是:"+u.pwd);
}

foreach(user u in list_u)
{
Console.WriteLine("用户名是:"+u.Name);
Console.WriteLine("编号是:"+u.Id);
Console.WriteLine("密码是:"+u.pwd);
}

数组

foreach