mirror of
https://github.com/digital-jellyfish/Virtu.git
synced 2024-11-23 19:30:59 +00:00
365e5723c1
--HG-- extra : convert_revision : svn%3Affd33b8c-2492-42e0-bdc5-587b920b7d6d/trunk%4050811
38 lines
948 B
C#
38 lines
948 B
C#
using System;
|
|
using System.Threading;
|
|
|
|
namespace Jellyfish.Library
|
|
{
|
|
public sealed class Lazy<T> where T : class
|
|
{
|
|
public Lazy(Func<T> initializer)
|
|
{
|
|
_initializer = initializer;
|
|
}
|
|
|
|
public T Value
|
|
{
|
|
get
|
|
{
|
|
if (_value == null)
|
|
{
|
|
T value = _initializer();
|
|
if (Interlocked.CompareExchange(ref _value, value, null) != null)
|
|
{
|
|
var disposable = value as IDisposable; // dispose preempted instance
|
|
if (disposable != null)
|
|
{
|
|
disposable.Dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
return _value;
|
|
}
|
|
}
|
|
|
|
private Func<T> _initializer;
|
|
private T _value;
|
|
}
|
|
}
|