I have written an article on Visual Studio Code Snippets - how to use them, and how to create your own. I have provided there a couple of snippets that I use on daily basis. You can also check Mads Kristensen's blog where he provides a snippet for "safe events". Today I have thought of yet another useful snippet...
There often happens that you need to have some kind of property that does some calculation yet you do not want it to be a method. Maybe it is because of the ease of use or some other reason? But as far as best practice tells you, the property is expected not to perform any kind of lenghty operation. So in order to be able to make a property that does some time consuming task the result of which can be then quickly retrieved any time, you do a what is called a Lazy Loading or Lazy Initialization - one of the most useful patterns.
Suppose that we have a tree structure of some operations. Each operation is either an atomic operation or a composite operation consisting of sub operations - a simple composite pattern. Each atomic operation has a property that shows the number of arguments the operation requires to execute. So to count a total number of arguments required by the tree of operations you would have to recursively sum the number of parameters from all sub operations. And that is the operation that can take some time to perform given a large tree of operations. So there is a a choice of making exposing this functionality as a method or doing the Lazy Initialization.
If you go the "lazy" way, you will end up with something like this:
If using 2.0 version of the .NET Framework, you can do better and use the Nullable type for the field. And that is exactly the code snippet I provide. With it you can create such a lazy initialized property very quickly. Just type: "propn" and what you get is:
Just fille the green places.
I have added the snippet to my collection of Snippets.zip (2.23 KB). Feel free to use it.