Assuming you want to simply return a unique incrementing integer value, the simplest safe approach is probably to use a private static counter and a private static lock object. Something like:
private static int s_UniqueCounter; // starts at 0 by default
private static readonly object s_UniqueCounterLock = new object();
public static int GetUnique()
{
lock (s_UniqueCounterLock)
{
s_UniqueCounter++;
return s_UniqueCounter;
}
}
It's up to you to make sure the particular lock object is used to protect any other access to the static counter member (which is why they are declared private, of course, so the class which owns them controls any access). It probably shouldn't be accessed anywhere else, for this use, but you might have something look at the current value to see how many times it has been called. That should probably be protected by the lock as well (to make sure the result is current):
internal static int QueryUniqueCount() // or this could be private, or public
{
lock (s_UniqueCounterLock)
{
return s_UniqueCounter;
}
}