Is lazy instantiation about using less code but getting the same result? Surely this is generally a good thing to do (providing making the code to short / efficient doesn't damage readability/maintainability).
Please refer to this lazy instantiation:
public sealed class Singleton
{
private Singleton()
{
}
public static Singleton Instance { get { return Nested.instance; } }
private class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested()
{
}
internal static readonly Singleton instance = new Singleton();
}
}
There is no private property of Instance
(I know it's implicit) - is it that which makes it lazy - the fact we don't have a setter within the public static Singleton Instance
property?
Lazy<T>
? have a look at this article: msdn: lazy initialization – SojournLazy<Singleton>
: https://mcmap.net/q/86663/-when-should-i-use-lazy-lt-t-gt – Sojourn