Musings
Public · Protected · Private
Lazy Loading in C#
-
2012-05-13 11:46If you have a class human class Human { int hLeft; int hRight; int lLeg; int lRight; void Dance() { } public Human(int x) { System.Threading.Thread.Sleep(500); } public Human() { System.Threading.Thread.Sleep(500); } } and constructor is costly (FOR NOW ... anyway object is needed when it really has to 'dance') Instead of creating the object immediately ..(may be your application is starting/loading/for some reason it will be busy ...) Instead of using these constructors... Human human = new Human(); Human human2 = new Human(); Use ... Lazy<Human> lazyhuman = new Lazy<Human>(); Lazy<Human> lazyhuman2 = new Lazy<Human> (delegate() { return new Human(2); });
This blog is frozen. No new comments or edits allowed.