I have a very basic question:
Say I have below class which is registered as singleton, and a method. What happens when Create()
is hit by multiple threads? Do they all the threads get access to this method at same time? If 10 threads hit this method, does 10 numbers
variable get created and disposed off once the scope of method ends?
As this class doesn’t have any class level properties, I assume it is thread safe?
// registration
builder.Services.AddSingleton<ISingleton, SingletonTest>();
public interface ISingleton
{
int Create(int number);
}
public class SingletonTest : ISingleton
{
public int Create(int number)
{
List<int> numbers = new List<int>();
numbers.Add(number);
return numbers.Count;
}
}