Search code examples
c#wcf-ria-services

How to identify different instance of object?


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?


Solution

  • 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).