i want to understand that code. I think T must be IContinentFactory's implemented class but i don't understand to end of the new() keyword.
class AnimalWorld<T> : IAnimalWorld where T : IContinentFactory, new()
{
.....
}
i want to understand that code. I think T must be IContinentFactory's implemented class but i don't understand to end of the new() keyword.
class AnimalWorld<T> : IAnimalWorld where T : IContinentFactory, new()
{
.....
}
T: new() means that type T has to have a parameter-less constructor.
By that you actually specify that you can write T param = new T(); in your implementation of AnimalWorld<T>
The constraint new() means that the type T must have a public parameterless instance constructor. This includes all value types, but not all classes. No interface or delegate type can have such a constructor. When the new() constraint is present, T can never be an abstract class.
When new() is present, the following code is allowed inside the class:
T instance = new T();
class AnimalWorld<T> : IAnimalWorld where T : IContinentFactory, new()
Here is what the declaration means:
AnimalWorld is a class with a generic type parameter TAnimalWorld must implement IAnimalWorldT must implement IContinentFactoryT must have a no-argument constructor (that's what the new is for).