The following is a thread safe singleton done in C#9 or higher
public sealed class DataContainer
{
private static readonly Lazy<DataContainer> Lazy = new(() => new DataContainer());
public static DataContainer Instance => Lazy.Value;
public int ValueToShare { get; set; }
}
Any project in your solution that the above class is in scope can be used as followed
Set
DataContainer.Instance.ValueToShare = 10;
Read
MessageBox.Show($"{DataContainer.Instance.ValueToShare}");
Or perhaps
int total = 9 + DataContainer.Instance.ValueToShare;
If using .NET Framework, you need to modify the int of Lazy to
private static readonly Lazy<DataContainer> Lazy = new Lazy<DataContainer>(() => new DataContainer());