我创建了一个列表:
Listemployees = new List 我想实现这个目标:
Employee e1 = new Employee(Job.Employee, "Name", "C Sharp", "Oracle", "SQL");我目前有这个:
Employee e1 = new Employee(Job.Employee, "Name", Skills.CSharp);这是
Employee
类中的代码public enum Job { Employee, Supervisor, Administrator }; public enum Skills {CSharp, SQL, PHP, Javascript, Web, Python, Oracle, CPlus, Perl }; protected Job job; protected String employeeName; protected String employeeName; public Job Job { get { return job; } protected set { job = value; } } public String EmployeeName { get { return employeeName; } protected set { employeeName = value; } } public Skills Skills { get {return skills; } }我希望能够输入尽可能多的"技能",因为我使用了枚举,因此只能在员工e1中输入一项技能.
我如何在列表/构造函数中放入一系列"技能"?
1> Jakub Lortz..:您可以使用
params
数组参数将可变数量的Skills
参数传递给构造函数:public Employee(Job job, string name, params Skills[] skills)您还必须修改
skills
字段以保存集合Skills
而不是单个集合.如果您在创建员工后不添加/删除技能,或者IList
您需要修改集合,则可以将其设为数组.例如:
public class Employee { private List_skills; // skills stored as a private List // to allow modification inside Employee class public Employee(Job job, string name, params Skills[] skills) { _skills = new List (skills); ... } public IReadOnlyList Skills // publicly visible as a read-only list { get { return _skills.AsReadOnly(); } } ... }