Solved

c# local variable lazy initialization

Posted on 2012-03-28
2
503 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 75

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Offline Apllication 5 70
scanning dentists xray (the small ones) 3 53
Run time Error 4 47
C# Desktop Application 3 30
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

786 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