如何允许C#添加另一种类型的结构以添加到内部或添加相同类型的列表?

如何允许C#添加另一种类型的结构以添加到内部或添加相同类型的列表?,c#,.net,dynamic-data,C#,.net,Dynamic Data,我有一个列表,我想在其中添加所有动物,甚至我可以添加它们或将它们添加到整个列表中 我如何做一些事情,他们允许添加列表或老鼠,这并不是我需要添加任何类型动物的唯一方法 这意味着我可以两者兼得 List<animal> animal = new List<animal>(); animal.Add(new rat()); animal.Add(new List<Elephant>()); List animal=new List(); 动物。添加(新鼠())

我有一个
列表
,我想在其中添加所有动物,甚至我可以添加它们或将它们添加到整个列表中

我如何做一些事情,他们允许添加
列表
老鼠
,这并不是我需要添加任何类型动物的唯一方法

这意味着我可以两者兼得

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

animal.Add(new rat());
animal.Add(new List<Elephant>());
List animal=new List();
动物。添加(新鼠());
添加(新列表());
我还需要一件事,所有的动物都是在动物列表中找到的所有动物。我不需要计算所有的对象,我需要计算每个单独添加或添加整个列表的动物

有人能用C语言解释一下代码吗

在.NET 4.0中,由于通用协方差,您还可以执行以下操作:

animal.AddRange(new List<Elephant>()); 
animal.AddRange(新列表());
但在以前版本的框架中没有

在.NET 4.0中,由于通用协方差,您还可以执行以下操作:

animal.AddRange(new List<Elephant>()); 
animal.AddRange(新列表());

但在以前版本的框架中没有。

以两种不同的动物为例,我认为动物的基类是有意义的,并为大象和动物派生一个单独的类。一种不太新颖的方法是创建一个通用的对象列表。不确定您的项目是什么,因此根据具体情况,您需要选择要使用的实现。将每个对象添加到泛型列表中,并在将其与GetType()方法一起使用之前检查类型

下面是一个使用派生类的示例。您可以将基类更改为上面讨论的接口或抽象类。稍后我将提供一个使用泛型对象的示例

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;
    }
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
//使用派生方法。
列出动物=新列表();
添加(新老鼠(“老鼠的名字”);
添加(新大象(“大象的名字”);
foreach(动物中的动物a)
{
控制台写入线(
格式(“动物名称:{0}”)、a.Name);
}
}
}
公营动物
{
公共动物(字符串名称)
{
this.Name=Name;
}
公共字符串名
{
得到;
私人设置;
}
}
公营大象:动物
{
公共大象(字符串名称)
:base(名称)
{
}
公共字符串AnimalProps
{
得到;
设置
}
}
公营鼠:动物
{
公共Rat(字符串名称)
:base(名称)
{
}
公共字符串道具
{
得到;
设置
}
}

以两种不同的动物为例,我认为动物的基类是有意义的,并为大象和动物派生一个单独的类。一种不太新颖的方法是创建一个通用的对象列表。不确定您的项目是什么,因此根据具体情况,您需要选择要使用的实现。将每个对象添加到泛型列表中,并在将其与GetType()方法一起使用之前检查类型

下面是一个使用派生类的示例。您可以将基类更改为上面讨论的接口或抽象类。稍后我将提供一个使用泛型对象的示例

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;
    }
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
//使用派生方法。
列出动物=新列表();
添加(新老鼠(“老鼠的名字”);
添加(新大象(“大象的名字”);
foreach(动物中的动物a)
{
控制台写入线(
格式(“动物名称:{0}”)、a.Name);
}
}
}
公营动物
{
公共动物(字符串名称)
{
this.Name=Name;
}
公共字符串名
{
得到;
私人设置;
}
}
公营大象:动物
{
公共大象(字符串名称)
:base(名称)
{
}
公共字符串AnimalProps
{
得到;
设置
}
}
公营鼠:动物
{
公共Rat(字符串名称)
:base(名称)
{
}
公共字符串道具
{
得到;
设置
}
}

下面是一个使用对象列表的示例。我建议不要使用这种实现,因为一般来说,基类/抽象类/接口类和派生类更干净,尽管我见过这样的情况

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;
        }
    }
public Form2()
{
初始化组件();
列表对象=新列表();
添加(新老鼠(“老鼠的名字”);
添加(新大象(“大象的名字”);
foreach(对象中的对象o)
{
if(o.GetType()==typeof(Rat))
{
大鼠r=o为大鼠;
控制台写入线(
格式(“rat的名称:{0}”,r.Name));
}
else if(o.GetType()==typeof(大象))
{
象e=o为象;
控制台写入线(
Format(“象的名称:{0}”,e.Name));
}
}
}
公营大象
{
公共大象(字符串名称)
{
this.Name=Name;
}
公共字符串名
{
得到;
私人设置;
}
公共字符串AnimalProps
{
得到;
设置
}
}
公营鼠
{
公共Rat(字符串名称)
{
this.Name=Name;
}
公共字符串名
{
得到;
私人设置;
}
公共字符串道具
{
得到;
设置
}
}

下面是一个使用对象列表的示例。我建议不要使用这种实现,因为一般来说,基类/抽象类/接口类和派生类更干净,尽管我见过这样的情况

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;
        }
    }
public Form2()
{
初始化组件();
列表