简体   繁体   中英

C# Class Add ID from one class to another

i have one class named with Animal, with Name, Type, Gender, and i have another class named with AnimalList, i need to know how can i add Animals to my AnimalList, this is my class animal(im in console application): Filename Animal.cs

class Animal
{
  public string Name {get; set;}
  public string Type {get; set;}
  public string Gender {get; set;}

  public Person(string name, string type, string gender)
  {
       Name = name;
       Type = type;
       Gender = gender;
  }
}

And my class AnimalList: FilenameAnimalList.cs

class AnimalList: List<Animal>
{
     what should i do to add Animals to this list
}

shouldn't be better like this?

public new void Add(Animal value)
{
}

Why do you have the constructor for Person in your Animal class?

Do like this:

var animals = new AnimalList();
var animal = new Animal();
animals.Add(animal);

You have to make your Animal class public:

public class Animal
{
}

Instantiate your AnimalList class somewhere(for example in main method) and then add the instances of an Animal class.

for example:

var animals = new AnimalList();

animals.Add(new Animal() {
   Name = "", 
   Type = "", 
   Gender = ""});

First of all make your class Public And do this:

 AnimalList animalList = new AnimalList();
            Animal animal = new Animal();
            animal.Name = "x";
            animal.Type = "Y";
            animal.Gender = "M/F";
            animalList.add(animal);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM