0

I am in a situation where I need need to keep track of all the variables a object, and therefore I need a way to

So I need a way to get a unique id (string or int, doesn't matter) that represent a object.

string obj1 = "test";
string obj2 = "test";

In this example obj1.id must be different from obj2.id. So obj.GetHashCode() is not what I am looking for...

I am considering using the pointer as an identifier like this:

GCHandle handle = GCHandle.Alloc(obj1 , GCHandleType.WeakTrackResurrection);
int obj1Id = GCHandle.ToIntPtr(handle).ToInt32();

But I don't know if that is a good idea, maybe it can change, if it gets moved in memory (don't know if that ever happens though). My gut tells me it's not a good solution... Or is it fine and safe to do it that way?

Or is there another way to get a unique identifier?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
BjarkeCK
  • 5,694
  • 5
  • 41
  • 59
  • 3
    I think you're trying to solve the wrong problem. You've decided the *solution* to a problem you've not told us about involves having this ability to track references - and now you're having a problem with implementing this "solution". Can you tell us what the overall goal is instead? – Damien_The_Unbeliever Feb 05 '14 at 07:33
  • This is actually a special case to strings in C#. If the strings are exactly the same, they will actually point to the same object in memory if created like this. – Øyvind Bråthen Feb 05 '14 at 07:33
  • I've tested exactly this example, and both pointer id, where different from eachother. – BjarkeCK Feb 05 '14 at 07:34
  • @Damien_The_Unbeliever Well i guess what i'm really asking is to review the solution i have come up with. And tell me if it is safe enough to do it this way? Is the id garenteed to be unique? and is it garenteed to stay the same for as along as the object exsist? I was also hoping for a more elegant solutions. But maybe it already is. id dont know – BjarkeCK Feb 05 '14 at 07:38
  • @BjarkeCK In your tests, have you found the ids to be the same? Using a `Dictionary` or as @TGH suggested, a `KeyValue` may be better – rhughes Feb 05 '14 at 07:44
  • My review is "I don't know what problem you're trying to solve, but my immediate gut feeling is that you're solving it the wrong way" - if you're hoping for more elegant solutions, tell us what *problem* having these "unique identifiers" is allowing you to *solve*. – Damien_The_Unbeliever Feb 05 '14 at 07:44
  • Correct me if I'm wrong here but It looks like the pointers will always be different since you're asking to allocate two new Handles. The allocated handles won't be the same either way. So comparing the two string handles will always turn out to be different. – Vincent Feb 05 '14 at 07:52
  • @Damien_The_Unbeliever sorry, thought my question was clear, my bad... The reason why i need is a much longer story. – BjarkeCK Feb 05 '14 at 07:58
  • @Vincent That is the point :) they need them to be different. – BjarkeCK Feb 05 '14 at 07:59
  • 1
    @BjarkeCK, my point is that the IDs here: string obj1 = "lol"; int obj1Id = GCHandle.ToIntPtr(GCHandle.Alloc(obj1, GCHandleType.WeakTrackResurrection)).ToInt32(); int obj2Id = GCHandle.ToIntPtr(GCHandle.Alloc(obj1, GCHandleType.WeakTrackResurrection)).ToInt32(); will also be different, which is not what you want since the object is exactly the same. Or did I missread your question? And this seems related: http://stackoverflow.com/questions/750947/net-unique-object-identifier – Vincent Feb 05 '14 at 08:02

0 Answers0