−Check whether two accesses return the same object reference.
using System;
using System.Collections.Generic;
using System.Linq;
Console.WriteLine(ReferenceEquals(AppCatalog.Instance, AppCatalog.Instance));
sealed class AppCatalog
{
private static readonly Lazy<AppCatalog> shared = new(() => new AppCatalog());
private AppCatalog() { }
public static AppCatalog Instance => shared.Value;
public string Version => "v1";
}
+Use AppCatalog, a catalog containing only a version string, as the shared object and compare two accesses by reference.
using System;
using System.Collections.Generic;
using System.Linq;
Console.WriteLine(ReferenceEquals(AppCatalog.Instance, AppCatalog.Instance));
// Own construction so callers do not create separate catalogs.
sealed class AppCatalog
{
private static readonly Lazy<AppCatalog> shared = new(() => new AppCatalog());
private AppCatalog() { }
// Lazy<T> synchronizes initialization; callers using this static field receive the same object.
public static AppCatalog Instance => shared.Value;
public string Version => "v1";
}