четверг, 28 апреля 2011 г.

Lazy

Recently a Lazy<T> class was introduced in TP in Tp.Utils namespace (Tp.Utils project). This is a helper for lazy initialized variables, similar to .NET 4 Lazy<T>, but more simple and not thread-safe.

The public API of Lazy<T> consists of:
public static class Lazy
{
 public static Lazy<TCreate<T>(Func<T> loader);
}

public class Lazy<T>
{
 public Lazy(Func<T> loader);
 public T Value { get; }
 public static implicit operator T(Lazy<T> lazy);
} 
 
First static class is a helper class to simplify Lazy<T> creation.

Example:

Lazy<Uri> lazyUri = Lazy.Create(() => HttpContext.Current.Request.Url);

Uri value = lazyUri.Value;
Uri casted = lazyUri;
 
The highlighted function would be called only whether the Value property will be used or lazy variable will be converted to its generic type.

After .NET 4 migration the Lazy<T> will be replaced with CLR .NET Lazy<T>.

Комментариев нет:

Отправить комментарий