Search code examples
.netstatic-classes

c# vb: do we really need System.Lazy?


Do we really need System.Lazy? Let's say my class library have 100 static classes and each static class uses an average of 100 static System.Lazys = 10000 System.Lazys that have to be initiated when a program that uses my class library starts?

I mean we usually don't only use 1 class library, say we import 15 .dlls each of them have a ton of static System.Lazys.. I'm pretty sure its gonna take a long time initiating all those System.Lazys, not to mention its gonna hoard a ton of memory as well (since each have its own System.Func let's put it at 50 bytes each =500kb in memory)

So I was wondering.. is it best to just do it the "old" way (like using a boolean to keep track of whether something is initated)


Solution

  • You should only use Lazy if 1) there's a good chance the instance won't be needed, and 2) the cost of spinning up the instance is high enough to justify the overhead.

    If you have 100 static lazy members in a class, you're almost certainly doing it wrong.