简体   繁体   English

我如何允许C#添加另一种类型的结构以在其中添加或添加相同类型的列表?

[英]how i can allow C# to add another type of structure to add within or add list of same type?

i have a List<animal> where i want to add all animal their even i can add them or add them whole list. 我有一个List<animal> ,我想在其中添加所有动物,甚至我可以添加它们或将它们添加到整个列表中。

how i can do something that they allow to add the List<rat> or rat their is not only one i need to add any type of animal in it. 我如何做一些他们允许添加List<rat>rat的事情,我不仅需要在其中添加任何类型的动物。

means i can allow both 意味着我可以两者都允许

List<animal> animal  = new List<animal>();

animal.Add(new rat());
animal.Add(new List<Elephant>());

i need a thing more that all animal is all animal found in animal list. 我还需要更多的东西,就是所有动物都是动物清单中的所有动物。 i not need to count all object i need to count Every animal who add seprately or add whole list. 我不需要计算所有对象我需要计算每个单独添加或添加整个列表的动物。

Can someone explain the code in C#. 有人可以解释C#中的代码。

List<animal> animal  = new List<animal>();
animal.Add(new Animal());
animal.AddRange(new List<animal>());

Of course if the types you are willing to add don't have a common base parent you cannot use a generic list. 当然,如果您要添加的类型没有通用的基本父代,则不能使用通用列表。 You might use an ArrayList which allows for storing any types. 您可以使用允许存储任何类型的ArrayList


UPDATE: 更新:

If Rat and Elephant both derive from Animal you can always do 如果RatElephant都来自Animal那么您总是可以

List<animal> animal  = new List<animal>();
animal.Add(new Rat());

And in .NET 4.0 thanks to generic covariance you can also do: 在.NET 4.0中,由于通用协方差,您还可以执行以下操作:

animal.AddRange(new List<Elephant>()); 

but not in previous versions of the framework. 但在该框架的早期版本中没有。

For your example with two different kinds of animals, I think a base class of animal makes sense, and derive a separate class for Elephant and Animal. 对于您使用两种不同种类动物的示例,我认为动物的基类很有意义,并为大象和动物派生了一个单独的类。 A less novel approach, though doable is creating a generic list of objects. 尽管可行,但不太新颖的方法是创建对象的通用列表。 Not sure what your project is, so depending on the situation, you'll need to choose the implementation to use. 不确定项目是什么,因此根据情况,您需要选择要使用的实现。 Add each object to the generic list and check the type before using it with GetType() method. 将每个对象添加到通用列表中,并在将其与GetType()方法一起使用之前检查类型。

Here's an example of using derived class though. 这是使用派生类的示例。 You could change the base class to be an interface or abstract class as discussed above. 您可以将基类更改为接口或抽象类,如上所述。 I'll provide an example shortly for using generic objects. 我将在短期内提供一个使用通用对象的示例。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // Using derived way.
        List<Animal> animals = new List<Animal>();
        animals.Add(new Rat("the rat's name"));
        animals.Add(new Elephant("the elephant's name"));

        foreach (Animal a in animals)
        {
            Console.WriteLine(
                string.Format("Name of animal: {0}"), a.Name));
        }
    }
}

public class Animal
{
    public Animal(string name)
    {
        this.Name = name;
    }

    public string Name
    {
        get;
        private set;
    }
}

public class Elephant : Animal
{
    public Elephant(string name)
        :base(name)
    {

    }

    public string AnimalProps
    {
        get;
        set;
    }
}

public class Rat :Animal
{
    public Rat(string name)
        :base(name)
    {

    }

    public string RatProps
    {
        get;
        set;
    }
}

Here's an example with using a list of objects. 这是使用对象列表的示例。 I'd advise against this implementation, as generally a base/abstract/interface class and derived classes is cleaner, though I have seen cases where something like this is required. 我建议不要使用此实现,因为通常,基类/抽象类/接口类和派生类更干净,尽管我已经看到了需要类似此类的情况。

public Form2()
    {
        InitializeComponent();

        List<object> objects = new List<object>();
        objects.Add(new Rat("the rat's name"));
        objects.Add(new Elephant("the elephant's name"));

        foreach (object o in objects)
        {
            if(o.GetType() == typeof(Rat))
            {
                Rat r = o as Rat;

                Console.WriteLine(
                    string.Format("Name of rat: {0}", r.Name));
            }
            else if(o.GetType() == typeof(Elephant))
            {
                Elephant e = o as Elephant;

                Console.WriteLine(
                    string.Format("Name of elephant: {0}", e.Name));
            }
        }
    }

    public class Elephant
    {
        public Elephant(string name)
        {
            this.Name = name;
        }

        public string Name
        {
            get;
            private set;
        }

        public string AnimalProps
        {
            get;
            set;
        }
    }

    public class Rat
    {
        public Rat (string name)
        {
            this.Name = name;
        }

        public string Name
        {
            get;
            private set;
        }

        public string RatProps
        {
            get;
            set;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中将类类型列表值添加到另一个类类型列表 - How to add class type list value to another class type list in C# 如何在C#中的另一个排序列表中添加排序列表? - How to add Sorted List within another Sorted List in C#? 如何在C#库中将UserControl添加到另一个? (异常:无法解析类型...) - How to add a UserControl to another within a C# library?? (Exception: Could not resolve type…) 如何在另一个包含c#中列表的类类型列表中添加Element - How to add Element inside another list of class type which contain the list in c# 如何使用Add-Type通过System.Windows.Forms命名空间添加C#代码? - How can I Use Add-Type to add C# code with System.Windows.Forms namespace? 如何在C#中添加到结构列表 - how add to list of structure in c# 如何在 List 的公共属性中添加值<string>输入 c#?</string> - How to add value in a public property of List<string> type in c#? 如何以C#Windows窗体在运行时添加具有相同控件类型的相同类型的面板? - How to add same type of panel having same type of controls at runtime in c# windows form? 我如何在2秒内增加精度C# - How can I add a precision within 2 seconds c# 如何在 C# 中将子对象添加到父泛型类型 - How can I add child object to the parent generic type in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM