C# - 如何将列表添加到列表中 [英] C# - how to add a list to the list

查看:147
本文介绍了C# - 如何将列表添加到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一类对象:文本,数字和图片列表。我不知道我是否做得很好,但我想把它存放在一个列表中以便更好地使用。不幸的是,我不知道如何在列表中添加列表以及如何在以后显示它。循环为?任何人都可以帮我吗?



我尝试过:



I created a class of objects: text, number and list of pictures. I do not know if I'm doing well, but I wanted to store it in a list for a better using. Unfortunately, I do not know how to add a list to the list and how to show it later. Loop for? Can anyone help me with this?

What I have tried:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

<pre>namespace DynamicObjectList
{
    class Products
    {
        public int id { get; set; }
        public string title { get; set; }
        public string amount { get; set; }
        public string price { get; set; }
        public List<Image> images { get; set; }
	}
}

...

...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DynamicObjectList
{
    public partial class Form1 : Form
    {
        public int nr = 0;

        List<Products> prodList = new List<Products>(3);

        List<string> imgList1 = new List<string>();

        public Form1()
        {
            InitializeComponent();
        }

        private void button_Add_Click(object sender, EventArgs e)
        {
            nr++;
            Products prod = new Products();
            prod.id = nr;
            prod.title = "Product " + nr.ToString();
            prod.amount = (Convert.ToInt16("2") * nr).ToString();
            prod.price = (Convert.ToDouble("10,00") * nr).ToString();
            prod.images = imgList1;
            prodList.Add(prod);

        }

        private void button_Show_Click(object sender, EventArgs e)
        {


            for (int i = 0; i < prodList.Count; i++)
            {
                Products p = prodList[i];

                textBox_Wyswietla.Text += String.Format("ID: {0}, Title: {1}, Amount: {2}, Price: {3}\r\n", p.id, p.title, p.amount, p.price);

                List<string> img = new List<string>();
                img = p.images;
                for (int j = 0; j < img.Count; j++)
                {
                    textBox_Wyswietla.Text += img[j] + "\r\n";
                }
            }

        }
...

推荐答案

List类是强类型的,这意味着它只接受最初定义为保持列表中相同类的对象(或者从该类派生的对象)。

例如:

The List class is strongly typed, which means it only accepts objects which are of the same class at the list was originally defined to hold (or which are derived from that class).
For example:
class A {}
class B : A {}
class C : B {}
List<A> dataA = new List<A>();
List<B> dataB = new List<B>();
List<C> dataC = new List<C>();

dataA 可以保存类的实例A,B和C; dataB 可以包含B和C的实例,但不包含A;和 dataC 只能保存C的实例,但不能保存A或B.



所以你不能创建一个列表< Products> 并期望它除了产品实例之外还有其他任何东西 - 你肯定无法添加列出< Products> ,因为它们甚至没有轻微相关!



如果您要做什么是添加一个不同的图像列表(实际上是你的例子中的字符串列表)到每个产品(并请,帮助我们一个忙,摆脱那里的s - 它暗示它是一个集合类而不是一个单个对象)然后你就在那里 - 你需要做的就是在创建产品时创建一个新的图像列表实例:

dataA can hold instances of classes A , B, and C; dataB can contain instances of B and C, but not A; and dataC can only hold instances of C, but not A or B.

So you can't create a List<Products> and expect it to hold anything other than Products instances - and you certainly can't add a List<Products> to it as they are not even slightly related!

If what you are trying to do is add a different List of Images (which is actually a List of strings in your example) to each Products (and please, do us all a favour and get rid of the "s" there - it implies it's a collection class instead of a single object) then you are halfway there - all you need to do is create a new instance of the images list when you create the Product:

Products prod = new Products();
prod.id = nr;
prod.title = "Product " + nr.ToString();
prod.amount = (Convert.ToInt16("2") * nr).ToString();
prod.price = (Convert.ToDouble("10,00") * nr).ToString();
prod.images = new List<string>(imgList1);
imgList1.Clear();

(清除以删除下次的物品)

(The Clear to to remove the items for the next time)


你好,



查看此 C#4 - 元组 [ ^ ]



这对你有帮助。



--RA
Hello,

check this C# 4 - Tuples[^]

This will help you.

--RA


这篇关于C# - 如何将列表添加到列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆