I have several classes that require a lot of parameters. I just created an example with two parameters to demonstrate my problem.
public class Zoo
{
public List<Animal> Animals { get; set; }
public Zoo()
{
Animals = new List<Animal>();
}
public void AddAnimal(string name, int age)
{
Animals.Add(new Animal(name, age));
}
public void AddAnimal(params ...)
{
Animals.Add(new Animal(...));
}
}
public class Animal
{
public string Name { get; set; }
public int Age { get; set; }
public Animal(string name, int age)
{
Name = name;
Age = age;
}
}
If I want to add Animalto mine Zoo, I have 2 options.
- Pass all parameters to the method
AddAnimal - Add object
AnimaltoList
As I said, I have many parameters and many "animals".
Is it possible to refer to constructor parameters Animalwithout manually adding them to the AddAnimal method? (See: -> pseudo code <-in the first block of code)
But I want to save a method call.
Zoo zoo = new Zoo();
zoo.AddAnimal("Tom", 8);
Thanks in advance, Yang
Edit:
For some reason, I want to prevent using one of the following methods:
zoo.AddAnimal(new Animal("Ben", 9));
zoo.AddAnimal(new Animal { "Ben", 9 });
, : c & p ?