i have called multiple threads which generates a number but i wanted to generate a unique no with all threads,( suppose one thread generates a no ie ABC1 but second thread must generate ABC2 and so on)
You could use Interlocked.Increment
, which will complete a thread safe increment on the counter.
public class Person
{
private static int _counter;
public string GetNewId()
{
int id = Interlocked.Increment(ref _counter);
return $"ABC{id}";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With