Pass parameters to constructor, when initializing a lazy instance
问题 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 ? 回答1: 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