1

Suppose I have a class like:

public class MyClass{
  public MyClass(){
    ...
  }
  ...
}

then I can create a instance of the class like:

MyClass instance1 = new MyClass();

When debug the code, whatever the instance is, it is always go through same code. How can I identify if the instance is same with others especially for some .NET system object? For example, WCF Ria Service has a basic class DomainContext, how can I know if an instance of DomainContext is new or same for the application?

KentZhou
  • 24,805
  • 41
  • 134
  • 200
  • Are you comparing two instances to see if they are the same one? – Kendall Frey Jan 04 '12 at 14:38
  • 1
    Is your question you want to be able to programatically determine if two references not the same? Or how in the debugger you can tell which instance you are dealing with when in an instance method? – James Michael Hare Jan 04 '12 at 14:40
  • If you are wanting to tag a reference just for the debugger (and don't want to add to the memory footprint of your type or are using a type you have no control over) then there's an answer on SO: http://stackoverflow.com/questions/4251450/uniquely-identifying-reference-types-in-the-debugger – James Michael Hare Jan 04 '12 at 14:49

3 Answers3

2

If your question is how to tell which reference you are looking at in an instance method, you can add this to your watch list, and then right click on the watch list entry and select "Make Object ID" which will tag the reference with a unique ID.

Follow the steps in this similar SO question: Identifying Unique References in Debugger

If your question is how to tell if two references are the same, you can use ReferenceEquals() to compare them (or == if you know it hasn't been overloaded for the type).

Community
  • 1
  • 1
James Michael Hare
  • 37,767
  • 9
  • 73
  • 83
  • Is the unique ID that gets added in the watch list related to the default `toString() and `hashCode()`? – cdeszaq Jan 04 '12 at 16:08
1

For debugging purposes you could simply number your instances like so:

public class MyClass{
  private static int instanceCounter = 0;

  private int instanceNumber;

  public MyClass(){
        instanceNumber = Interlocked.Increment(instanceCounter);
  }
  ...
}
N_A
  • 19,799
  • 4
  • 52
  • 98
  • 1
    Incidentally, ++ is not thread-safe. You'd want an interlocked increment. – James Michael Hare Jan 04 '12 at 14:43
  • Actually, I wouldn't use `lock`, it's relatively heavy for just holding an increment, use the return value from the `Interlocked.Increment()`, it returns the new value: `public MyClass() { instanceNumber = Interlocked.Increment(ref instanceCounter); }` – James Michael Hare Jan 04 '12 at 15:09
  • 1
    This works, of course, but it does add memory space to the class, which if he only wants to keep them apart for debugging purposes, could be overkill. If he wants to unique ID them for the whole program this is fine, but if he just wants to ID them for debugging I'd just use an Object ID. – James Michael Hare Jan 04 '12 at 15:12
  • @JamesMichaelHare The drawback to that is that it doesn't automatically appear in the watch list. – N_A Jan 04 '12 at 15:24
0

All the instances have an identifier of some sort. In a properly implemented class (or one that doesn't override the hash function), getting the hash code will indicate if the instance is the same or different. In may cases, the default string representation of the object will consist of the class name and an identifier for the particular instance. Simply printing the instance to the console may be sufficient to tell instances apart.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
  • What about it? Each instance still has an object identifier, which is what @James was referring to in his answer. – cdeszaq Jan 04 '12 at 16:07