【发布时间】:2021-09-05 14:28:09
【问题描述】:
这是头等舱
public class Employees
{
public int empID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Position { get; set; }
public string Email { get; set; }
}
public class list
{
static public List<Eployees> _employees = new List<Employees>();
public List<Employees> GetList()
{
return _employees;
}
public string fname;
public void Add()
{
int result = _employees.Count(x => x.empID == x.empID);
int id = result + 1; //auto increment for empID
_employees.Add(new Employees
{
empID = id,
FirstName = fname
});
}
这是第二课
class Class1
{
public static main void Main(string[] args)
{
ManageEmployee();
}
static void AddEmployees
{
list ll = new list();
Console.WriteLine("Enter FirstName ");
string name = Console.ReadLine();
ll.name = name;
ll.Add();
string view = Console.ReadLine();
foreach(var Employees in list._employees)
{
Console.WriteLine(Employees.empID + " | " + Employees.FirstName);
}
string reset = Console.ReadLine();
AddEmployees();
}
}
我是 C# 新手,基本上,我想知道有没有更好的方法将元素添加到列表中?我看到我的老师做了一些不同的事情,但我不明白她是怎么做到的,我一直在寻找更好的添加方法
【问题讨论】:
-
就个人而言,我不会命名
classlist。当您开始使用代码时,这会变得非常混乱,因为正如您在自己的使用中所看到的那样,您在list类中创建了一个List<Eployees>。 -
看到
AddEmployee没有定义,我会说是的,有比使用未定义方法更好的方法。