Note that there are some explanatory texts on larger screens.

plurals
  1. POc# vb: Should we use System.Lazy for resource-intensive task? (when threading is not needed)
    primarykey
    data
    text
    <p>I'm wondering is there some kind of JIT-hack going on with System.Lazy to make things more performant or is it purely a "normal class"?</p> <p>From the page <a href="http://msdn.microsoft.com/en-us/library/dd642331.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/dd642331.aspx</a> it says:</p> <blockquote> <p>Use an instance of Lazy(Of T) to defer the creation of a large or resource-intensive object or the execution of a resource-intensive task, particularly when such creation or execution might not occur during the lifetime of the program.</p> </blockquote> <p>but i can defer the execution of a resource-intensive task using a simple boolean flag couldn't i? So what exactly is the difference? (other than System.Lazy has additional overheads for no apparent "syntax sugar" gains)</p> <p>With a simple boolean flag its simply:</p> <pre><code>if (!deferred) { //run resource-intensive task } </code></pre> <p>Edit: </p> <p>here's an example</p> <pre><code>class Human{ System.Lazy&lt;String&gt; name = new System.Lazy&lt;String&gt;(() =&gt; { //code here takes 4 seconds to run return "the value"; }); String Name { get { return name.Value; } } } </code></pre> <p>vs</p> <pre><code>class Human { String name; bool name_initiated; String Name { get { if (!name_initiated) { //code here takes 4 seconds to run name = "the value"; name_initiated = true; } return name; } } } </code></pre> <p>6 May: now i use this alot. And i really mean alot alot. i use it whenever i need to cache data (even when the computation is 0.1 second or lesser). Hence my question, should i be worried? Now i know you will tell me to profile the app, but im building the library first before i build the app and by that time if the app has problems that would mean Major change </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload