I need to attach a unique identifier to objects at runtime. The identifier must be unique for the duration of the application. I plan to do this my having a private member
I would recommend using an integer value, and auto-incrementing it on assignment. You could use Interlocked.Increment to make this operation thread safe.
Most likely, a 32bit integer will be large enough for this task. I would recommend something like:
private static newObjectId = int.MinValue;
private static int GetNextId()
{
return Interlocked.Increment(ref newObjectId);
}
You can then use that in your base class for assigning a new, unique identifier.
To generate unique ids for objects you could use the aptly named ObjectIDGenerator that we conveniently provide for you:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.objectidgenerator.aspx
Note that, as the comment points out, the object id generator keeps a reference to the object alive, so it is only suitable for objects that you know are going to survive the life of the application anyway. If you intend to use this thing on more ephemeral objects then it is not a good solution.
You could build your own object ID generator that kept weak references if you wanted; it wouldn't be that difficult.
Do you need the identifier to be unique across all objects or just within a specific type?
You have a couple of options:
Object.GetHashCode()
, this will give you a pretty (but not 100%) reliable identifier. Note, though, that (per the docs), this is not guaranteed to be unique. Your chances of hitting a duplicate, though, are pretty low.private static long lastObjectId
in your class. Within the base constructor, use Interlocked.Increment(ref lasObjectId) as the value for the current object.If the uniqueness is just for the life of the application, can't you use a 32-bit integer, initialized to zero, and then simply incremented with each object allocation?
There's no need to worry about TickCount or anything like that. The number "2" is unique among numbers; it's as different from "1" and "3" as it is from "1,203,718" if all you're testing for is equality.