What is lazy instantiation
Asked Answered
P

5

6

Is lazy instantiation about using less code but getting the same result? Surely this is generally a good thing to do (providing making the code to short / efficient doesn't damage readability/maintainability).

Please refer to this lazy instantiation:

public sealed class Singleton
{
    private Singleton()
    {
    }

    public static Singleton Instance { get { return Nested.instance; } }

    private class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly Singleton instance = new Singleton();
    }
} 

There is no private property of Instance (I know it's implicit) - is it that which makes it lazy - the fact we don't have a setter within the public static Singleton Instance property?

Peculiar answered 25/9, 2012 at 13:29 Comment(4)
msdn.microsoft.com/en-us/library/dd997286.aspxAppendicectomy
Why don't you use Lazy<T>? have a look at this article: msdn: lazy initializationSojourn
I have read that - the problem is I don't understand without testing and at work we're only on .Net 2.0 (so I can't use <T>) - Don't ask!!Peculiar
@DaveRook: Here's a Lazy<Singleton>: https://mcmap.net/q/86663/-when-should-i-use-lazy-lt-t-gtSojourn
U
14

Lets say we have a field of a type that is expensive to construct

class Foo
{
    public readonly Expensive expensive = new Expensive();
    ...
}

The problem with this code is that instansiating Foo incurs the performance cost of instansiating Expensive - whether-or-not the Expensive field is ever accessed. The obvious answer is to construct the instance on demand or lazily instansiate the field:

class Foo
{
    Expensive _expensive;
    public Expensive
    {
        get
        {
            if (_expensive == null) _expensive = new Expensive();
            return _expensive;
        }
    }
    ...
}

This is lazy instansiation.

Unconnected answered 25/9, 2012 at 13:37 Comment(3)
Are you suggesting that you can make each field a lazy load as well (providing the property is a class)Peculiar
There would be no point in lazily instansiating anything that was not expesive to initialise. That is the whole point. You can do this with any class you want by using the accessor in this way.Unconnected
There are some good answers to my question - but this really made the penny drop - and made me think of more ways to implement it. Although it may be easier to think of it as efficient loading instead of lazy loading! :)Peculiar
C
6

Lazy initialization is a practice whereby you only load or initialize an object when you first need it.

Potentially, this can give you a big performance boost, especially if you have a vast amount of components in your application.

Look at the Wikipedia page for a greater insight (it features coded examples).

Cameleer answered 25/9, 2012 at 13:32 Comment(2)
I had seen that Wiki page - but lazy loading, lazy initialisation and lazy evaluation, eager initialiation etc - made me think (at this early stage of my learning) that they were all different. I'm starting to realise that lazy init. / lazy load is the same thingPeculiar
Well, it's semantics. It can mean different things. A class can be lazily init, but maybe the class also lazy loads media data lazily as well.Episode
E
5

No, lazy instantiation means not spending any time and resources creating something until you actually need it.

In your singleton example, the instance is just an empty reference, until it's actually used. When it's used, then you spend the resources to instantiate the object with a new.

Episode answered 25/9, 2012 at 13:31 Comment(3)
Ah - hence the fact it is in the static method - call it only when it's needed without anything done prior!Peculiar
Yeap. Also for a singleton, future calls will just access that one already created instance.Episode
Yeah, I know about the singleton now! It was the loading type I didn't get, thank you.Peculiar
E
2

It's lazy because the instance of the class Singleton isn't created until the first time you ask for it.

Embrace answered 25/9, 2012 at 13:31 Comment(0)
S
2

Lazy initialization of an object means that its creation is deferred until it is first used.

For complete reference see msdn post Lazy Initialization

In your above code, the instance of the singleton class is not created until you call it. So, your program will not use resources until your code gets called.

Spinose answered 25/9, 2012 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.