Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# equivalent of Python's id()?

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.

like image 966
wes Avatar asked Dec 05 '22 00:12

wes


1 Answers

You can test object identity with Object.ReferenceEquals.

like image 51
Marcelo Cantos Avatar answered Dec 07 '22 13:12

Marcelo Cantos