Covariant use of generic Lazy class in C#
c#, c#-4.0, covariance, lazy-initialization
Solution
Well, you won't be able to use it exactly as is. Probably the simplest refactor would be to have it accept a `Func<Animal>` instead of a `Lazy`. Then you could pass in a lambda that fetches the value of a `Lazy`. `Func` is covariant with respect to it's return type.
Problem
Assuming that this applies: ``` public class Cat : Animal { } ``` and assuming that I have a method: ``` public void Feed(Animal animal) { ... } ``` And I can call it this way: ``` var animal = new Cat(); Feed(animal); ``` How can I get this working when `Feed` is refactored to only support `Lazy<Animal>` as parameter? I'd like to pass in my `var lazyAnimal = new Lazy<Cat>();` somehow. This obviously doesnt work: ``` var lazyAnimal = new Lazy<Cat>(); Feed(lazyAnimal); ```