Question based on two articles:
Actually, I remembered that structures have a slightly different situation with calling a static constructor (it is called before accessing a static field, but not called when creating an instance of a structure). Accordingly, there was a desire to put the Instance property inside the structure. At first, I wanted to woo something with the beauty of addressing it, but then I changed my mind and decided to ask for the very concept - does it give anything besides lazy initialization (by the way, is it really guaranteed here) and are there any side effects ?
using System; struct FieldLikeSingletonWrapper { public class FieldLikeSingleton { internal FieldLikeSingleton() { Console.WriteLine("FieldLikeSingleton.ctor"); } public void Foo() { Console.WriteLine("Foo"); } } public static FieldLikeSingleton Instance { get; } = new FieldLikeSingleton(); } class Program { static void Main(string[] args) { Console.WriteLine("Inside Main()"); if (args.Length == 42) { FieldLikeSingletonWrapper.Instance.Foo(); } } } By the way, at first it seemed obvious to me that making the singleton itself a structure is a terrible idea, since the structure will be copied. But then I thought that I could declare methods to throw methods through the structure (yes, this is a minus), but use the hidden nested class:
using System; struct FieldLikeSingleton { private class FieldLikeSingletonImpl { internal FieldLikeSingletonImpl() { Console.WriteLine("FieldLikeSingleton.ctor"); } public void Foo() { Console.WriteLine("Foo"); } } private static FieldLikeSingletonImpl instance = new FieldLikeSingletonImpl(); public static FieldLikeSingleton Instance { get; } public void Foo() { instance.Foo(); } } class Program { static void Main(string[] args) { Console.WriteLine("Inside Main()"); if (args.Length == 42) { FieldLikeSingleton.Instance.Foo(); } } } What are the pros and cons of these approaches compared to the usual field-like?