创建对象的树列表

时间:2014-08-08 16:24:42

标签: c# list object

我想使用对象列表

创建层次结构树

树是这样的 我无法创建动态列表

Image

//code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class Student
    {
        string studentname;
        class Department
        {
            string departmentname;
            class Subject
            {
                string subjectname;
            }
            List<Subject> sub = new List<Subject>();
        }
        List<Department> dep = new List<Department>();

        public void getdata()
        {
            List<Student> stu = new List<Student>();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

您可以从:

开始
class Student
{
    string studentname;

    public Student(string studentname) {
        this.studentname = studentname;
    }

    public List<Department> Departments = new List<Department>();
}

class Department
{
    string departmentname;

    public Department(string departmentname) {
        this.departmentname = departmentname;
    }

    public List<Subject> Subjects = new List<Subject>();
}

class Subject
{
    string subjectname;

    public Subject(string subjectname) {
        this.subjectname = subjectname;
    }
}

以这种方式使用上述结构:

Student s = new Student("Student 1");
Department d = new Department("Department A");
s.Departments.Add(d);
d.Subjects.Add(new Subject("Subject A"));