When I programmed in C/C++, I often include pointer values in tracing. This helped correlate objects between trace messages. In C#, I don't have a pointer value available I can trace. Are there any good alternatives for traceable object IDs??
I don't know offhand how to get the underlying memory address, but even if I did that wouldn't help much as the GC could move objects around.
A tip: inside Visual Studio, you can right click on the object in the locals/watch window, and select "Make Object ID" to give the object a unique tag.
http://blogs.msdn.com/saraford/archive/2008/09/16/did-you-know-how-to-create-an-object-id-to-keep-track-of-your-objects-314.aspx
Use Object.GetHashCode(). The GetHashCode() method can be accessed from all objects since every class in C# derives from Object. But its guaranteed to be unique only when Object.GetHashCode() is called and its result is used in some way for the generation of hash codes for user defined classes. Please note this is not an absolute guarantee you may still end up getting the same Hashcode for more than one object.
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