DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Lazy in C# 4.0

Jalpesh Vadgama user avatar by
Jalpesh Vadgama
·
Jan. 04, 13 · Interview
Like (0)
Save
Tweet
Share
7.85K Views

Join the DZone community and get the full member experience.

Join For Free
Before C# 4.0 there was no on demand initialization by default. So at the time of declaration we need to create a value or object will be null or we have to create on demand initialization via coding.. But with C# 4.0 we now have lazy class. As per MSDN it support lazy initialization so it means if we use lazy class then it will initialize the object at the time of demand.

It is very useful for UI responsiveness and other scenario's.  Lazy initialization delays certain initialization and  it’ll improve the start-up time of a application. In the earlier framework if we need this kind of functionality we have to do it manually via coding but from C# 4.0 onwards it have Lazy<T> class. With the Help of this we can improve the responsiveness of C# application. Following is a simple example.
using System;
namespace LazyExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Lazy<Customer> lazyCustomer = new Lazy<Customer>();
            Console.WriteLine(string.Format("Is customer initialized :{0}", lazyCustomer.IsValueCreated));
            Customer customer = lazyCustomer.Value;
            Console.WriteLine(string.Format("Is string value initialized :{0}", lazyCustomer.IsValueCreated));
        }
    }
    public class Customer
    {
        public Customer()
        {
            Console.WriteLine("Customer Intialized");
        }
    }
}

As you can see in the above example, I have created a class called Customer with public constructor and in constructor it prints that Customer class is initialized and that constructor will be called. In the main method of console application I have created lazy customer object with the help of lazy in c#. I have used IsValueCreated property to check whether value initialized or not and I am printing that values before the use and after use of lazy object.

Let’s run example via pressing F5 and following is a output as expected.



You can see in above example first value was not initialize so IsValueCreated returned false. After use of customer its initialized and its returns true.

Note: C# lazy initialization is complex process internally so it will have a overhead if you define all the object as lazy, So whenever the object could take time to initialize at that time you can use Lazy in c#. For example reading a file will take more time due to IO operations as well if you are fetching data from the third party source or web service where it can take longer time to initialize then you can use lazy over there.

That’s it. Hope you like it. Stay tuned for more.. .
Lazy initialization

Published at DZone with permission of Jalpesh Vadgama, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Quick Pattern-Matching Queries in PostgreSQL and YugabyteDB
  • Agile Transformation With ChatGPT or McBoston?
  • What Is Policy-as-Code? An Introduction to Open Policy Agent
  • What Was the Question Again, ChatGPT?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: