Hello @Markus Freitag
Have you considered using a thread-safe Singleton? The following is based off this web page sixth version.
public class Address
{
public string Type { get; set; }
public string Company { get; set; }
public string Number { get; set; }
public string Street { get; set; }
public string City { get; set; }
}
public partial class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime? DateOfBirth { get; set; }
public IList<Address> Addresses { get; set; }
}
Singleton
public sealed class Sample
{
private static readonly Lazy<Sample> Lazy =
new Lazy<Sample>(() =>
new Sample());
/// <summary>
/// Entry point to access information in this class
/// </summary>
public static Sample Instance => Lazy.Value;
public List<Person> People { get; set; }
private Sample()
{
People = new List<Person>();
}
}
Add in one thread
Sample.Instance.People.Add(new Person() {FirstName = "Karen", LastName = "Payne"});
Read in another thread (or add etc if you want)
foreach (var person in Sample.Instance.People)
{
Console.WriteLine($"{person.FirstName} {person.LastName}");
}