Solved

c# local variable lazy initialization

Posted on 2012-03-28
2
485 Views
Last Modified: 2012-04-02
can someone tell me the pros and cons of lazy initializing local variables?  my main concern about using this technique is that there is always the possibility that a variable doesn't get lazy initialized because it's being used in a whole bunch of places.  so as opposed to writing lazy initialization code in multiple places possibly, why not just initialize it the moment you declare it.
0
Comment
Question by:mmingfeilam
2 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 37780207
I'll have to defer for the "cons", but regarding this statement: " so as opposed to writing lazy initialization code in multiple places possibly"...

I would say that if you are duplicating your "lazy initialization" code in multiple places, then you are probably doing something wrong. Lazy initialization just means that you don't instantiate something until you need it. For local variables, I would wonder under what circumstances you were contemplating lazy initialization. If you have a lengthy function, then it may be the case that your function requires refactoring anyway. If you have a normal function, then any expensive-to-create objects which you have declared locally I would expect to have been coded in such a manner that you really wouldn't need lazy initialization--but I'm rather sleepy, so I'm sure I could be overlooking something.

From what I am reading, lazy initialization is akin to the Singleton pattern. I would inquire as to whether or not a pattern like that would suit your environment.
0
 

Author Comment

by:mmingfeilam
ID: 37784695
what i mean by lazy init is this:

MyClass var = null;

...
if(condition1)
  var = new MyClass();
else if(condition2)
...
else if(condition3)
  var = new MyClass();
else
 ...
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now