Sunday 6 April 2014

C# Lazy Class

When you create a variable in C# it immediately reserves a space in memory even if you don't assign a value to it. For example: if you create an int variable it immediately reserves 32 bit of memory. It is very rare for this to be an issue but there may be cases when you need to work with a class that consumes enormous amount of memory.

In these cases you can use the Lazy class to prevent the object from being loaded into memory until it is needed. This is called Lazy instantiation. If you follow good coding practices, its extremely unlikely that you'll ever need to make use of the lazy class.

An important note when working with Lazy objects is that their constructor methods will not run until they are loaded into memory.

No comments:

Post a Comment