public class myClass
{
public myClass(String InstanceName)
{
Name = InstanceName;
}
public String Name { get; set; }
}
// Now using myClass lazily I have:
Lazy<myClass> myLazy;
Console.WriteLine(myLazy.Value.Name);
My question is how to pass InstanceName
to myClass
constructor when we are using a lazy instance ?
Try this:
Lazy<myClass> myLazy = new Lazy<myClass>(() => new myClass(InstanceName));
Remember that the expression is evaluated lazily, so if you change the value of the variable InstanceName
before the constructor is called it might not do what you expect.