5

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?

Dave
  • 8,163
  • 11
  • 67
  • 103
  • 1
    http://msdn.microsoft.com/en-us/library/dd997286.aspx – user981225 Sep 25 '12 at 13:32
  • 1
    Why don't you use [`Lazy`](http://msdn.microsoft.com/en-us/library/dd642331.aspx)? have a look at this article: [msdn: lazy initialization](http://msdn.microsoft.com/en-us/library/dd997286.aspx) – Tim Schmelter Sep 25 '12 at 13:33
  • 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 ) - Don't ask!! – Dave Sep 25 '12 at 13:33
  • @DaveRook: Here's a `Lazy`: http://stackoverflow.com/a/6847882/284240 – Tim Schmelter Sep 25 '12 at 13:44

5 Answers5

13

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.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • 1
    Are you suggesting that you can make each field a lazy load as well (providing the property is a class) – Dave Sep 25 '12 at 13:40
  • 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. – MoonKnight Sep 25 '12 at 13:43
  • 1
    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! :) – Dave Sep 25 '12 at 13:44
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).

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
  • 1
    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 thing – Dave Sep 25 '12 at 13:36
  • 1
    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. – Kache Sep 25 '12 at 13:37
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.

Kache
  • 15,647
  • 12
  • 51
  • 79
  • Ah - hence the fact it is in the static method - call it only when it's needed without anything done prior! – Dave Sep 25 '12 at 13:32
  • Yeap. Also for a singleton, future calls will just access that one already created instance. – Kache Sep 25 '12 at 13:36
  • Yeah, I know about the singleton now! It was the loading type I didn't get, thank you. – Dave Sep 25 '12 at 13:36
2

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

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
James Cronen
  • 5,715
  • 2
  • 32
  • 52
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.

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154