.NET Thread Safe Lock on Property

3/31/2016 4:01:08 PM

The following is a property that is thread safe and only therefore only accessed by 1 thread at a time. Following the same pattern to add a Setter.

//thread safe lock on a .NET property (static in this case)      
private static readonly object _Locker__MyList = new object(); 
private static List<int> _MyList;       
public static List<int> MyList
{
	get
	{
		lock (_Locker__MyList)
		{
			if (_MyList == null)
			{
				_MyList = new List<int>();
				_MyList.Add(1);
				_MyList.Add(2);
				_MyList.Add(3);
				_MyList.Add(5);
				_MyList.Add(8);
			}

			return _MyList;
		}
	}            
}