static Lazy<T> LazyLoader<T>() where T : new()Usage of the above code,
{ return new Lazy<T>(() => new T()); } static Lazy<T> LazyLoader<T>(Func<T> lazyInitialisationBlock) where T : new() { return new Lazy<T>(lazyInitialisationBlock); } static List<string> InitialisationBlock() { return new List<string> { "One", "Two" }; }
static void Main(string[] args)
{ Lazy<ClassForLazyLoad> loader = LazyLoader<ClassForLazyLoad>(); Console.WriteLine("{0}", loader.Value.Name); Lazy<List<string>> listLoader = LazyLoader<List<string>>(InitialisationBlock); Console.WriteLine("{0}", ReferenceEquals(listLoader, null) ? "Loading hasn't been finished yet" : "Loading has been finished"); Array.ForEach(listLoader.Value.ToArray(), item => Console.WriteLine(item.ToString())); }
related classes,
public class ClassForLazyLoad { public string Name { get { return GetType().FullName; } } }
No comments:
Post a Comment