简体   繁体   English

.NET 4中的延迟初始化

[英]Lazy initialization in .NET 4

What is lazy initialization. 什么是延迟初始化。 here is the code i got after search google. 这是我搜索谷歌后得到的代码。

class MessageClass
{
    public string Message { get; set; }

    public MessageClass(string message)
    {
        this.Message = message;
        Console.WriteLine("  ***  MessageClass constructed [{0}]", message);
    }
}

Lazy<MessageClass> someInstance = new Lazy<MessageClass>(
    () => new MessageClass("The message")
    );

why should i create object in this way....when actually we need to create object in this way......looking for answer. 我为什么要以这种方式创建对象....实际上我们需要以这种方式创建对象......寻找答案。

The purpose of the Lazy feature in .NET 4.0 is to replace a pattern many developers used previously with properties. .NET 4.0中的Lazy功能的目的是替换许多开发人员以前使用属性的模式。 The "old" way would be something like “老”的方式就像是

private MyClass _myProperty;

public MyClass MyProperty
{
    get
    {
        if (_myProperty == null)
        {
            _myProperty = new MyClass();
        }
        return _myProperty;
    }
}

This way, _myProperty only gets instantiated once and only when it is needed. 这样, _myProperty只会在需要时进行一次实例化。 If it is never needed, it is never instantiated. 如果它永远不需要,它永远不会被实例化。 To do the same thing with Lazy , you might write 要和Lazy做同样的事情,你可以写

private Lazy<MyClass> _myProperty = new Lazy<MyClass>( () => new MyClass());

public MyClass MyProperty
{
    get
    {
        return _myProperty.Value;
    }
}

Of course, you are not restricted to doing things this way with Lazy , but the purpose is to specify how to instantiate a value without actually doing so until it is needed. 当然,您并不局限于使用Lazy以这种方式执行操作,但目的是指定如何在不需要实际实例化值之前直到需要它。 The calling code does not have to keep track of whether the value has been instantiated; 调用代码不必跟踪值是否已被实例化; rather, the calling code just uses the Value property. 相反,调用代码只使用Value属性。 (It is possible to find out whether the value has been instantiated with the IsValueCreated property.) IsValueCreated该值是否已使用IsValueCreated属性进行实例化。)

"Lazy initialization occurs the first time the Lazy.Value property is accessed or the Lazy.ToString method is called. “第一次访问Lazy.Value属性或调用Lazy.ToString方法时会发生延迟初始化。

Use an instance of Lazy 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." 使用Lazy实例来推迟创建大型或资源密集型对象或执行资源密集型任务,尤其是在程序生命周期内可能不会发生此类创建或执行时。

http://msdn.microsoft.com/en-us/library/dd642331.aspx http://msdn.microsoft.com/en-us/library/dd642331.aspx

Check out msdn documentation over here : Lazy Initialization 在这里查看msdn文档: 延迟初始化

Lazy initialization of an object means that its creation is deferred until it is first used. 对象的延迟初始化意味着它的创建将延迟到首次使用。 Lazy initialization is primarily used to improve performance, avoid wasteful computation, and reduce program memory requirements. 延迟初始化主要用于提高性能,避免浪费计算,并减少程序内存需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM