C#为工作人员提供多维列表

时间:2015-05-08 07:15:17

标签: c# list multidimensional-array

我希望设置一个员工列表来插入多个数据项。例如,我想给员工一个ID,姓名,技术技能列表和个人技能列表。并非所有员工都拥有相同数量的技术技能或个人技能,但能够拥有每个人的倍数

所以一个例子是:

employeeID, employeeName, techSkill1, techSkill2, persSkill1

employeeID, employeeName, techSkill1, persSkill1, persSkill2

employeeID, employeeName, techSkill1, techSkill2, techSkill3, persSkill1

这甚至可能吗?

2 个答案:

答案 0 :(得分:4)

使用课程:

public class Employee
{
    /// <summary>
    /// employee's ID
    /// </summary>
    public int ID { get; set; }

    /// <summary>
    /// employuee's name
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// list of personal skills
    /// </summary>
    public List<string> PersSkills { get; private set; }

    /// <summary>
    /// list of tecnical skills
    /// </summary>
    public List<string> TechSkills { get; private set; }

    /// <summary>
    /// конструктор
    /// </summary>
    public Employee()
    {
        this.PersSkills = new List<string>();
        this.TechSkills = new List<string>();
    }

    /// <summary>
    /// конструктор
    /// </summary>
    public Employee(int id, string name, string[] persSkills, string[] techSkills)
    {
        this.ID = id;
        this.Name = name;
        this.PersSkills = new List<string>(persSkills);
        this.TechSkills = new List<string>(techSkills);
    }
}

用法:

List<Employee> employees = new List<Employee>();

employees.Add(new Employee(1, "Ivan", new string[] { "good friend" }, new string[] { "engineer" }));
employees.Add(new Employee(2, "Boris", new string[] { "personnel management", "tolerance" }, new string[] { "engineer", "programmer" }));

答案 1 :(得分:0)

是的,这是可能的,您可以这样做:

public List<Member> members = new List<Member>();

public Form1()
{
    InitializeComponent();

    Member me = new Member();
    me.ID = 3;
    me.Name = "Maarten";

    PersSkill skill1 = new PersSkill();
    skill1.Name = "Super Awsome Skill!";
    skill1.MoreInfo = "All the info you need";

    PersSkill skill2 = new PersSkill();
    skill1.Name = "name!";
    skill1.MoreInfo = "info";

    List<PersSkill> list = new List<PersSkill>();
    list.Add(skill1);
    list.Add(skill2);

    me.PersSkills = list;

}

public struct Member
{
    public int ID { get; set; }
    public string Name { get; set; }
    public List<TechSkill> PersSkills { get; set; }
    public List<TechSkill> TechSkills { get; set; }
}

public struct PersSkill
{
    public string Name { get; set; }
    public string MoreInfo { get; set; }
}

public struct TechSkill
{
    public string Name { get; set; }
    public string MoreInfo { get; set; }
}

P.S。使用@ General-Doomer的解决方案,这是一个更好的解决方案,但我会在这里留下我的答案,也许你可以用它做点什么/从中学习