I am working on an ASP.NET MVC application.
I have a ViewModel class for which I am trying to maintain a count in order to generate div ids which are used in a view:
public class ViewModel {
private static uint instanceCount;
private readonly uint id;
public ViewModel(){
id = instanceCount++;
}
public uint DivId
{
get { return id; }
}
}
Am I correct in thinking incrementing and assigning the static member is not threadsafe and thus not safe in a web application?
What is the best way to make this safe?
Thanks.
Yes, you are correct into thinking this.
By synchronizing the access:
or simply use a Guid: