i have a List<animal>
where i want to add all animal their even i can add them or add them whole list.
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.
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#.
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.
UPDATE:
If Rat
and Elephant
both derive from Animal
you can always do
List<animal> animal = new List<animal>();
animal.Add(new Rat());
And in .NET 4.0 thanks to generic covariance you can also do:
animal.AddRange(new List<Elephant>());
but not in previous versions of the framework.