我想使用参数
将列表传递给函数List<Employee> iList = new List<Employee>();
public void GetEmployeeData()
{
for (int i = 0; i < 10; i++)
{
iList[i].age = 12;
iList[i].Name = "Sandhya";
iList[i].cadder = "A+";
PassListData(iList[i].Name, iList[i].age, iList[i].cadder);
}
}
void PassListData(string Name, int age, string Cadder)
{
Console.WriteLine(Name);
Console.WriteLine(age);
Console.WriteLine(Cadder);
}
答案 0 :(得分:1)
您可以直接将列表传递给下面的函数。 首先将所有员工详细信息添加到列表中。然后将其作为列表传递
public void GetEmployeeData()
{
var iList = new List<Employee>();
for (int i = 0; i < 10; i++)
{
var employee = new Employee();
employee.age = 12;
employee.Name = "Sandhya";
employee.cadder = "A+";
iList.Add(employee);
}
PassListData(iList);
}
void PassListData(List<Employee> employees)
{
// loop through here
}
答案 1 :(得分:0)
简单地:
List<Employee> iList = new List<Employee>();
public void GetEmployeeData()
{
for (int i = 0; i < 10; i++)
{
iList[i].age = 12;
iList[i].Name = "Sandhya";
iList[i].cadder = "A+";
}
PassListData(iList);
}
void PassListData(List<Employee> list)
{
foreach(var e in list)
{
Console.WriteLine(e.Name);
Console.WriteLine(e.age);
Console.WriteLine(e.cadder);
}
}
答案 2 :(得分:0)
工作example
如果列表是班级列表,那么你可以这样做
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<Student> list = new List<Student>();
list.Add(new Student{name= "name1", age= 21, city= "city1"});
list.Add(new Student{name= "name2", age= 22, city= "city2"});
list.Add(new Student{name= "name3", age= 24, city= "city3"});
getList(list);
}
static void getList(List<Student> list)
{
foreach(var s in list)
{
Console.WriteLine("name = "+s.name + ", age= " + s.age +", city = "+ s.city);
}
}
}
public class Student{
public string name {set; get;}
public int age {set; get;}
public string city {set; get;}
}
结果
name = name1, age= 21, city = city1
name = name2, age= 22, city = city2
name = name3, age= 24, city = city3