C# 4.0 introduced new class Lazy
Lasy<T> example to load object lazily
public partial class LazyClass : System.Web.UI.Page
{
private Lazy<List<string>> lazyItems = null;
public LazyClass()
{
lazyItems = new Lazy<List<string>>(GetItems);
}
public List<string> GetItems()
{
List<string> items = new List<string>();
for (int i = 0; i < 10; i++)
{
items.Add(string.Format("Item {0}", i));
} return items;
}
public List<string> GetItemValues { get { return lazyItems.Value; } }
}
So in above example we have defined private Lazy<List<string>> lazyItems = null; and then in constructor we have delegate the call to GetItems method which will return list of items. But here it will not generate and return list until we call "lazyItems.Value".
0 Comments :
Post a Comment