Posts

Showing posts from November 30, 2018

Own implementation of Lazy object

Image
up vote 20 down vote favorite 7 The problem with the original Lazy in C# is that you have to put the initialization in the constructor if you want to refer to this . For me that is 95% of the cases. Meaning that I have to put half of my logic concerning the lazy property in the constructor, and having more boilerplate code. See this question: https://stackoverflow.com/questions/53271662/concise-way-to-writy-lazy-loaded-properties-in-c-sharp/53316998#53316998 Now I made a new version where I actually move the initialization part to the 'Get' moment. This saves boilerplate code and I can group everything about this property together. But: is the current implementation 'safe'? are there any important performance considerations vs the original one? is there anything else I'm missing that I should be con