Virtu/Library/Lazy.cs
Sean Fausett eaaab47a6c Initial checkin.
--HG--
extra : convert_revision : svn%3Affd33b8c-2492-42e0-bdc5-587b920b7d6d/trunk%4019155
2009-05-22 14:37:50 +00:00

38 lines
949 B
C#

using System;
using System.Threading;
namespace Jellyfish.Library
{
public 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)
{
IDisposable disposable = value as IDisposable; // dispose preempted instance
if (disposable != null)
{
disposable.Dispose();
}
}
}
return _value;
}
}
private Func<T> _initializer;
private T _value;
}
}