Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazy<T> reinitialization method?

Tags:

Does anyone know why Microsoft didn't include a Clear() method or IsDirty property or something of the like to the Lazy< T > class in the .NET Framework 4.0? (Updated question below)

We have a homebrew type that we have been using since .NET 3.5 where I work that does the same thing as Lazy< T > class yet allows you to have the instance re-evaluate the Lazy Func. We would like to replace our class with the new .NET one but this Clear() or IsDirty mechanism doesn't exist.

Let me rephrase the original question based on comments. Is there a way to reinitialize the Lazy< T > Func method without reinstantiating the class? If not, is there a way to implement it as an extension method or is just just a bad pattern to follow in the first place?

like image 486
Rob Packwood Avatar asked Feb 04 '11 21:02

Rob Packwood


2 Answers

Because it is impossible to make it thread-safe. Classes that guarantee that the programmer will shoot his leg off without any way to fix the problem don't belong in a framework. You are free to shoot your own leg off.

like image 150
Hans Passant Avatar answered Oct 18 '22 12:10

Hans Passant


What you want to do isn't Lazy initialization, it's something else. That's why it isn't on the Lazy<T> class.

like image 28
David Yaw Avatar answered Oct 18 '22 11:10

David Yaw