I have a class with the property that the dictionary:
public class Entity { public Dictionary<string, string> Name { get; set; } }
I would like to switch this property to use lazy initialization. I tried the following:
public class Entity { private Lazy<Dictionary<string, string>> name = new Lazy<Dictionary<string, string>>(() => new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)); public Dictionary<string, string> Name { get { return name; } set { name = value; } } }
This, of course, is a mistake, since the name and the name have different types. For the life of me, however, I cannot figure out how to indicate this correctly. All I really want is to have a name that stays null until I get access to it, and then create it the first time I read or write it.
c # properties lazy-initialization
Justin R.
source share