Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazy initialization in .NET 4

What is lazy initialization. here is the code i got after search google.

class MessageClass {     public string Message { get; set; }      public MessageClass(string message)     {         this.Message = message;         Console.WriteLine("  ***  MessageClass constructed [{0}]", message);     } }  Lazy<MessageClass> someInstance = new Lazy<MessageClass>(     () => new MessageClass("The message")     ); 

why should i create object in this way....when actually we need to create object in this way......looking for answer.

like image 374
Mou Avatar asked Jun 21 '11 12:06

Mou


People also ask

What is lazy initialization in C#?

Lazy initialization of an object means that its creation is deferred until it is first used. (For this topic, the terms lazy initialization and lazy instantiation are synonymous.) Lazy initialization is primarily used to improve performance, avoid wasteful computation, and reduce program memory requirements.

Is Lazy thread-safe?

Lazy<T> Per https://docs.microsoft.com/en-us/dotnet/api/system.lazy-1#thread-safety the default is thread safe, but can be disabled. If you don't opt-out it's thread safe.

Is lazy loading good C#?

Lazy loading is essential when the cost of object creation is very high and the use of the object is vey rare. So, this is the scenario where it's worth implementing lazy loading. The fundamental idea of lazy loading is to load object/data when needed.

Is lazy initialization a design pattern?

The lazy initialization pattern embodies the just-in-time philosophy of data delivery. It delays the construction of values or data structures until that data is actually needed. Lazy initialization is a popular design pattern in both Java and Objective-C.


1 Answers

The purpose of the Lazy feature in .NET 4.0 is to replace a pattern many developers used previously with properties. The "old" way would be something like

private MyClass _myProperty;  public MyClass MyProperty {     get     {         if (_myProperty == null)         {             _myProperty = new MyClass();         }         return _myProperty;     } } 

This way, _myProperty only gets instantiated once and only when it is needed. If it is never needed, it is never instantiated. To do the same thing with Lazy, you might write

private Lazy<MyClass> _myProperty = new Lazy<MyClass>( () => new MyClass());  public MyClass MyProperty {     get     {         return _myProperty.Value;     } } 

Of course, you are not restricted to doing things this way with Lazy, but the purpose is to specify how to instantiate a value without actually doing so until it is needed. The calling code does not have to keep track of whether the value has been instantiated; rather, the calling code just uses the Value property. (It is possible to find out whether the value has been instantiated with the IsValueCreated property.)

like image 166
Andrew Avatar answered Oct 04 '22 14:10

Andrew