Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C上使用Costum对象创建arrayList#_C# - Fatal编程技术网

C# 如何在C上使用Costum对象创建arrayList#

C# 如何在C上使用Costum对象创建arrayList#,c#,C#,我想在动态列表中添加对象以供使用。在其他情况下,我是C#.net新手,我尝试了以下代码: class DashboardClass { private int prix; private string name; private int quantity; public void SetInfo(string name, int prix, int quantity) { this.

我想在动态列表中添加对象以供使用。在其他情况下,我是C#.net新手,我尝试了以下代码:

class DashboardClass
    {
        private int prix;
        private string name;
        private int quantity;

        public void SetInfo(string name, int prix, int quantity)
        {
            this.prix = prix;
            this.quantity = quantity;
            this.name = name;
        }

        public int getprix()
        {
            return prix;
        }
        public string getname()
        {
            return name;
        }

        public int getquantity()
        {
            return quantity;
        }
    }
在我的主要表格上:

 DashboardClass Object = new DashboardClass();
         List<object> ProductList = new List<object>();
        DashboardClass item = Object.SetInfo("ala", 152, 1);
        ProductList.Add(item);
DashboardClass对象=新的DashboardClass();
List ProductList=新列表();
DashboardClass项=Object.SetInfo(“ala”,152,1);
产品列表。添加(项目);

请说明如何修改我制作Productlist列表的代码。

将您的设置信息发送给构造函数

class DashboardClass
    {
        private int prix;
        private string name;
        private int quantity;

        public DashboardClass(string name, int prix, int quantity)
        {
            this.prix = prix;
            this.quantity = quantity;
            this.name = name;
        }

        public int getprix()
        {
            return prix;
        }
        public string getname()
        {
            return name;
        }

        public int getquantity()
        {
            return quantity;
        }
    }
通过这种方式,您可以使用object通过get方法访问prix、name和quantity

List<DashboardClass> cls = new List<DashboardClass>();
            cls.Add(new DashboardClass("example", 1, 1));
            Console.WriteLine(cls[0].getprix());
            Console.Read();
List cls=new List();
添加(新的仪表板类(“示例”,1,1));
Console.WriteLine(cls[0].getprix());
Console.Read();
这里的cls[0]正在访问通用列表中的第一个对象


当列表中有更多对象时,只需使用foreach循环进行迭代

是否正在寻找类似的对象

class DashboardClass
{
    private int prix;
    private string name;
    private int quantity;

    public void DashboardClass(string name, int prix, int quantity)
    {
        this.prix = prix;
        this.quantity = quantity;
        this.name = name;
    }

    public int getprix()
    {
        return prix;
    }
    public string getname()
    {
        return name;
    }

    public int getquantity()
    {
        return quantity;
    }
}
然后

List ProductList=newlist();
仪表板类项目=新仪表板类(“ala”,152,1);
产品列表。添加(项目);
或键入方式(根据作者评论添加)

List ProductList=newlist();
仪表板类项目=新仪表板类(“ala”,152,1);
产品列表。添加(项目);

您确定要的是
对象的列表,而不是
仪表板类的列表吗?我想要一个可以通过对象访问的对象。例如,name
List<object> ProductList = new List<object>();
DashboardClass item = new DashboardClass("ala", 152, 1);
ProductList.Add(item);
List<DashboardClass> ProductList = new List<DashboardClass>();
DashboardClass item = new DashboardClass("ala", 152, 1);
ProductList.Add(item);