2

Possible Duplicate:
.NET unique object identifier

Instead of re-inventing the wheel, is there a unique, immutable object ID I can access for each object instance in .NET?

I'm asking this because I find myself writing the same code many times: Add a field or a property named Id, with type of Guid. This is useful mainly for logging purpose.
Example:

Logger.Debug(string.Format("about to save data to file, my id is {0}", this.Id));

Is there something built in the .NET framework instead of coding and using this.Id?
Surely there's nothing immediate, but maybe something using .NET internals.

Update:

I intentionally don't want to use the GetHashCode technique, or any related hash code mechanism.

Community
  • 1
  • 1
Ron Klein
  • 9,178
  • 9
  • 55
  • 88
  • Also interesting and related: http://stackoverflow.com/questions/4994277/memory-address-of-an-object-in-c-sharp –  Apr 03 '12 at 06:33
  • Check [Jon's answer](http://stackoverflow.com/a/9789370/445517) on the duplicate question. That's the way to go. – CodesInChaos Apr 03 '12 at 08:21

1 Answers1

1

No. There is no such field.

Some "alternatives" may be using the object's [default] hash code or using ObjectIDGenerator, but these serve different purposes...

Community
  • 1
  • 1