John Thompson
John Thompson

Reputation: 263

In .Net How Do You Check Whether Two Identifiers Point at Same INSTANCE of An Object?

I keep wanting to know if two variables are pointing at the same instance of a class. Sounds obvious, but can it be done?

For example. Imagine you have the following code with an operator called "IsSameInstanceAs". Is there any way of fulfilling the logic of the "IsSameInstanceAs" operator as stated in the comments inside the if statements using .Net, C#:-

public class MyClass
{
    public String MyString;

    public static void TestForSameInstance()
    {
        MyClass myInstanceA = new MyClass();
        MyClass myInstanceB = myInstanceA;
        MyClass myInstanceC = new MyClass();

        myInstanceA.MyString = "A String";
        myInstanceC.MyString = myInstanceA.MyString;

        if (myInstanceA IsSameInstanceAs myInstanceB)
        {
            // These are the same instance so they will match and this if is true
        }

        if (myInstanceA IsSameInstanceAs myInstanceC)
        {
            // These are not the same instance so they will not match and this if is false
        }
    }
}

I believe this cannot be done, but if anyone knows better then please help. Remember, I do not want to compare object instances, I want to know if they are the same instance.

John Thompson

Upvotes: 3

Views: 1382

Answers (4)

Rune FS
Rune FS

Reputation: 21742

In general when you use the comparison == operator on objects that's exactly what it does However if type of the object implements a oveerload of the comparison operator this might not hold true in which case you can always use object.Reference.Equals(x,y)

So with most types almost all from the BCL (string being a notable exception)

you can do

x == y; //reference comparison for most reference types
object.ReferenceEquals(x,y); //works in all cases
x.Equals(y); //reference comparison for most reference types

The first and last differ in that in the first it's the compile time type that's important where in the latter it's the runtime type. There's also no guarantee that they will yield the same result even when the compile time and runtime types are the same (they should but nothing but good manners enforces this)

Since it's the compile time type of the expressions in the first example that's important you could rewrite it to

(object)x == (object)y; //always a reference comparison for reference types

I'd personally use object.ReferenceEquals and not the == with casts. To me the method shows the intend more clearly.

Upvotes: 0

Jainendra
Jainendra

Reputation: 25143

From MSDN:
The following example uses ReferenceEquals to determine if two objects are the same instance.

class MyClass {

   static void Main() {
      object o = null;
      object p = null;
      object q = new Object();

      Console.WriteLine(Object.ReferenceEquals(o, p));
      p = q;
      Console.WriteLine(Object.ReferenceEquals(p, q));
      Console.WriteLine(Object.ReferenceEquals(o, p));
   }
}

This will produce the following output:

True
True
False

ReferenceEquals(Object objA, Object objB) will return true if objA is the same instance as objB or if both are null; otherwise, false.

Upvotes: 0

Guffa
Guffa

Reputation: 700212

Use the ReferenceEquals method:

if (Object.ReferenceEquals(myInstanceA, myInstanceB))

From the documentation:

"Determines whether the specified Object instances are the same instance."

Upvotes: 0

RB.
RB.

Reputation: 37182

Object.ReferenceEquals is the relevant method.

Determines whether the specified Object instances are the same instance.

Upvotes: 2

Related Questions