c#在类中列出<t>

时间:2015-05-09 14:23:52

标签: c# list class

这是一个相当简单的问题,我有3个类(见下面的代码),称为项目,站点和井。项目类将是一个列表,但我希望它包含一个站点列表,然后站点列表将包含一个井列表。不确定是否允许这样做。我做了一些搜索,我看到的大多数解决方案都谈到了如何创建列表类型而不知道它将成为什么类。我知道我的目标是什么,所以这不是问题。

Just to give this a visual

    list<t> Projects
            list<t> Sites
                    list<t> Wells


namespace EDMNoCost
{
    public partial class Form1 : Form
    {
      public class wells
        {
            public string c_wellname { get; set; }
            public string well_id { get; set; }
            public string site_id { get; set; }


            public wells(string c_wellname, string well_id,string site_id)
            {
                this.c_wellname = c_wellname;
                this.well_id = well_id;
                this.site_id = site_id;
            }
        }
        public class sites
        {
            public string c_wellname { get; set; }
            public string site_id { get; set; }
            public string project_id { get; set; }


            public sites(string c_wellname, string site_id, string project_id)
            {
                this.c_wellname = c_wellname;
                this.site_id = site_id;
                this.project_id = project_id;
            }
        }

        public class projects
        {
            public string project_name { get; set; }
            public string project_id { get; set; }
            public string site_id { get; set; }




            public projects(string project_name, string project_id, string site_id)
            {
                this.project_name = project_name;
                this.project_id = project_id;
                this.site_id = site_id;
            }
        }
        public List<projects> projInfo = new List<projects>();
        public List<sites> siteInfo = new List<sites>();
        public List<wells> well_list = new List<wells>();
    }
 }

2 个答案:

答案 0 :(得分:0)

这完全合法,只需将它们添加为公共属性:

public class projects
{
    public List<sites> siteInfo { get; set; }
    ...
    public projects (string project_name, string project_id, string site_id)
    {
        this.project_name = project_name;
        this.project_id = project_id;
        this.site_id = site_id;
        this.siteInfo = new List<sites>(); // You can add stuff to the List here if you want, or you can just initialize it.
    }
}

您也可以为其他课程做同样的事情。

答案 1 :(得分:0)

拥有这种结构和你的关闭是合法的,你需要为项目和站点添加1个属性然后你需要更改每个构造函数来初始化属性

完成后,您可以像这样使用您的代码;

2个项目

第一个有两个网站

第二个有1个站点和1个井

using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {        
        static void Main(string[] args)
        {
            List<projects> projInfo = new List<projects>();

            projInfo.Add(new projects("1", "1", "1"));
            projInfo.Add(new projects("2", "2", "2"));

            projInfo[0].siteInfo.Add(new sites("10", "10", "10"));
            projInfo[0].siteInfo.Add(new sites("20", "20", "20"));

            projInfo[1].siteInfo.Add(new sites("30", "30", "30"));

            projInfo[1].siteInfo[0].well_list.Add(new wells("100", "100", "100"));
        }
    }

    public class wells
        {
            public string c_wellname { get; set; }
            public string well_id { get; set; }
            public string site_id { get; set; }

            public wells(string c_wellname, string well_id,string site_id)
            {
                this.c_wellname = c_wellname;
                this.well_id = well_id;
                this.site_id = site_id;
            }
        }
        public class sites
        {
            public string c_wellname { get; set; }
            public string site_id { get; set; }
            public string project_id { get; set; }
            public List<wells> well_list { get; set; } //new property

            public sites(string c_wellname, string site_id, string project_id)
            {
                this.c_wellname = c_wellname;
                this.site_id = site_id;
                this.project_id = project_id;
                well_list = new List<wells>(); //init the property
            }
        }

        public class projects
        {
            public string project_name { get; set; }
            public string project_id { get; set; }
            public string site_id { get; set; }
            public List<sites> siteInfo { get; set; } //new property

            public projects(string project_name, string project_id, string site_id)
            {
                this.project_name = project_name;
                this.project_id = project_id;
                this.site_id = site_id;
                siteInfo = new List<sites>(); //init the property
            }
        }

}