When is the lazy load?

I lazily load all my members. I have been doing this for a while and just take a lazy load to be a good thing at face value.

Let's say that

public class SomeClass
{
   public int anInt;
   public SomeReferenceType member1;

   public SomeClass()
   {
      //initialize members in constructor when needed (lazy load)
      anInt = new int();
      member1 = new SomeReferenceType();
   }
}

Are there any drawbacks to doing this this way? Is this the correct lazy loading model? Does it make sense to use a lazy type of value (with modern RAM it even matters)?


After what I learned from your answers, I would like to know if there is any difference between the above and this ...
public class SomeClass
    {
       public int anInt;
       public SomeReferenceType member1 = new SomeReferenceType();

       public SomeClass()
       {

       }
    }
+5
source share
6 answers

First of all, member initialization inside the constructor is not a lazy load.

Lazy Loading . .NET( , ):

public class SomeClass
{
    private object _lockObj = new object();
    private SomeReferenceType _someProperty;

    public SomeReferenceType SomeProperty
    {
        get
        {
            if(_someProperty== null)
            {
                lock(_lockObj)
                {
                    if(_someProperty== null)
                    {
                        _someProperty= new SomeReferenceType();
                    }
                }
            }
            return _someProperty;
        }
        set { _someProperty = value; }
    }
}

, .NET 4, Lazy<T> , .

-, - , , , , . , .

Lazy , (, ).

+13

. . , .

    private string _someField;

    public string SomeField
    {
        get 
        {
            // we'd also want to do synchronization if multi-threading.
            if (_someField == null)
            {
                _someField = new String('-', 1000000);
            }

            return _someField;
        }
    }

Lazy load - , , , , , , :

public class SomeClass
{
    private string _someField;

    private readonly object _lazyLock = new object();


    public string SomeField
    {
        get 
        {
            // we'd also want to do synchronization if multi-threading.
            if (_someField == null)
            {
                lock (_lazyLock)
                {
                    if (_someField == null)
                    {
                        _someField = new String('-', 1000000);
                    }
                }
            }

            return _someField;
        }
    }
}

, .NET 4.0 Lazy<T>, - .

public class SomeClass
{
    private readonly Lazy<string> _someField = new Lazy<string>(() => new string('-', 10000000), true);

    private readonly object _lazyLock = new object();


    public string SomeField
    {
        get
        {
            return _someField.Value;
        }
    }
}

, - , ( ), , . , , .

+6

, , . , .

, , - ?

, -, .

, .NET 4.0 Lazy, , :

public class Foo
{
    private Lazy<int> _value = new Lazy<int>(() => 3);

    public int Value { get { return _value.Value; } }
}

- LazyThreadSafetyMode, : , , -, , .

+5

.

, ( )

- :

private SomeClass _someRef = null;
public SomeClass SomeRef
{
  get
  {
    if(_someRef == null)
    {
       //initialisation just in case of access
       _someRef = new  SomeClass();
    }
    return _someRef;
  }
}
0

lazy- int :

private int? _heavyLoadedInt;

public int HeavyLoading
{
    get
    {
        if (_heavyLoadedInt == null)
            _heavyLoadedInt = DoHeavyLoading();
        return _heavyLoadedInt.Value;
    }
}

, , , : NULL ( ); null NULL .

, . new int() , 0. , ( ), .

0

, , . , , . - /

0

All Articles