Is there a C# equivalent to Python's id()? If not, how can I test if an object has changed? For example, in Python, an immutable object:
>>> s = "abc"
>>> id(s)
11172320
>>> s += "d"
>>> id(s)
18632928
and a mutable one:
>>> a = [1, 2, 3]
>>> id(a)
11259656
>>> a += [4]
>>> id(a)
11259656
Edit - Most of the answers so far are about comparison functions/operators but I'm more interested in the values that are being compared. I wasn't initially clear about that, and I'm accepting Object.ReferenceEquals
as the answer.
Well, you can use the Object.ReferenceEquals()
function:
Determines whether the specified Object instances are the same instance.
To borrow your Python example a little bit:
List<int> listA = new List<int> { 1, 2, 3 };
List<int> listB = listA;
listA.Add(4);
bool equal = object.ReferenceEquals(listA, listB);
equal
will be true
.