Lazy<T>.IsValueCreated 属性
定义
public:
property bool IsValueCreated { bool get(); };
public bool IsValueCreated { get; }
member this.IsValueCreated : bool
Public ReadOnly Property IsValueCreated As Boolean
属性值
如果为此 Lazy<T> 实例创建了值,则为 true;否则为 false。true if a value has been created for this Lazy<T> instance; otherwise, false.
注解
当实例发生延迟初始化时 Lazy<T> ,可能会导致创建值或引发异常。When lazy initialization occurs for a Lazy<T> instance, it may result in either a value being created or an exception being thrown. 如果引发异常,则实例的后续行为 Lazy<T> 取决于异常缓存是否生效。If an exception is thrown, subsequent behavior of the Lazy<T> instance depends on whether exception caching is in effect. 如果该 Lazy<T> 实例是使用未指定初始化函数的构造函数创建的,则异常缓存将不起作用。If the Lazy<T> instance was created by using a constructor that does not specify an initialization function, then exception caching is not in effect. 再次尝试初始化 Lazy<T> 可能会成功,并且在成功初始化后, IsValueCreated 属性将返回 true 。A subsequent attempt to initialize the Lazy<T> might succeed, and after successful initialization the IsValueCreated property returns true. 如果 Lazy<T> 实例是使用) 构造函数的参数指定 (指定的初始化函数创建的 valueFactory Lazy<T> ,则异常缓存由线程安全模式控制。If the Lazy<T> instance was created with an initialization function (specified by the valueFactory parameter of the Lazy<T> constructor), then exception caching is controlled by the thread safety mode.
如果模式为 LazyThreadSafetyMode.ExecutionAndPublication 或 LazyThreadSafetyMode.None ,则没有第二次机会初始化该 Lazy<T> 实例。If the mode is LazyThreadSafetyMode.ExecutionAndPublication or LazyThreadSafetyMode.None, there is no second chance to initialize the Lazy<T> instance. 如果发生异常,且在初始化函数中未处理,则会缓存该异常并在后续访问属性时重新引发该异常 Lazy<T>.Value 。If an exception occurs and is unhandled in the initialization function, that exception is cached and rethrown on subsequent accesses of the Lazy<T>.Value property. 如果引发异常,则不会创建任何值,因此在这种情况下,将 IsValueCreated 返回
false。No value is created if an exception is thrown, so in such cases IsValueCreated returnsfalse.如果模式为,则为 LazyThreadSafetyMode.PublicationOnly 运行初始化函数的第一个线程 (或无参数构造函数) 为实例创建值 Lazy<T> 。If the mode is LazyThreadSafetyMode.PublicationOnly, the first thread that succeeds in running the initialization function (or the parameterless constructor) creates the value for the Lazy<T> instance. 如果初始化函数在一个线程上引发异常,其他线程仍可以尝试初始化该 Lazy<T> 实例。If the initialization function throws an exception on one thread, other threads can still try to initialize the Lazy<T> instance. 在创建该值之前,该 IsValueCreated 属性将返回
false。Until the value is created, the IsValueCreated property returnsfalse.