这是我的类结构:
public class Emp_Details : IEnumerable
{
public List<Employee> lstEmployee{ get; set; }
}
public class Employee
{ public string Name{ get; set; }
public int Age{ get; set; }
public string Address{ get; set; }
}
这是我正在尝试做的事情:
Emp_Details obj = new Emp_Details();
obj.lstEmployee = new List<Employee>();
Employee objEmp= new Employee();
objEmp.Name="ABC";
objEmp.Age=21;
objEmp.Address="XYZ";
obj.lstEmployee.Add(objEmp);
foreach(var emp in obj)
{
//some code
}
我想在 List<Employee> lstEmployee
上使用 foreach
但是我越来越
Emp_Details does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'
谁能帮帮我?
最佳答案
只需从 Emp_Details
的声明中删除基接口(interface):
public class Emp_Details
{
public List<Employee> lstEmployee{ get; set; }
}
关于c# - 如何在类的列表类型上使用 foreach 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32267844/