private void MainMethod()
{
Lazy<MyClass> mClass = LazyClass1; /* Choose LazyClass1 or LazyClass2 */
Console.WriteLine();
Console.WriteLine("[#] Value Created: {0}", mClass.IsValueCreated);
Console.WriteLine("[#] Name: {0} [ID - {1}]", mClass.Value.Name, mClass.Value.ID);
Console.WriteLine("[#] Value Created: {0}", mClass.IsValueCreated);
}
#region Lazy<T> Code
Lazy<MyClass> LazyClass1 = new Lazy<MyClass>(() => { return new MyClass() {
ID = 1,
Name = "AceInfinity"
}; });
Lazy<MyClass> LazyClass2 = new Lazy<MyClass>(() => { return new MyClass(); });
public class MyClass
{
public int ID { get; set; }
public string Name { get; set; }
public MyClass() : this(0, "{Default}") { }
public MyClass(int vID, string vName)
{
Console.WriteLine("[!] Constructor called for initialization...");
ID = vID;
Name = vName;
}
}
#endregion