I think you are asking the wrong question. My answer is pretty detailed, but here is a brief summary:
- There is no direct equivalent to
id in C #. - You can get the address of an object in C # using pointers, but you should not use this to solve your problem.
- The
object.ReferenceEquals method does something similar to what you are trying to do. - The problem you are trying to solve does not actually exist in C # in the same way as in Python.
- The method of using
id also does not solve the problem in Python.
First, I'll post from the documentation for id , since most people do not seem to have read it. do read this because the rest of my answer makes sense if you know what id doing (and no, it doesn't generate a hash code):
Return the "identifier" of the object. This is an integer (or a long integer) that is guaranteed to be unique and constant for this object throughout its life. Two objects with non-overlapping lifetimes can have the same id () value.
CPython implementation details: this is the address of the object.
C # does not have an id equivalent, but you can get the address of an object by getting a pointer to the object in unsafe mode.
unsafe { IntPtr x = (IntPtr)(&v); Console.WriteLine(x); }
However, you should not do this in C #. You very rarely need to use unsafe C # code, and if you do, it can spread easily, since the code that calls your code also becomes unsafe. There are other ways to solve your problem that don't require unsafe code. In addition, in .NET (unlike Python) objects can be moved in memory by the garbage collector, so if you want one identifier to remain constant throughout the life of the object, the memory address is useless to you. Instead, you should create a readonly property called id , for example.
An alternative approach that is possible in C # without using unsafe code is to check if two variables have the same reference using object.ReferenceEquals . However, you probably shouldn't do that either. C # does not have the same ambiguity as in Python. To specify a variable pointing to a new object, you use the = operator. A method call will almost always not change the link, so you should not worry about that. In C # x += y; the operator is (almost) equivalent to x = x + y , and you cannot change it as you can in Python.
Also, as mentioned in the documentation, your Python example is wrong - id cannot be used reliably to check if an object has changed. Example:
>>> s='abc' >>> id(s) 37822208 >>> s+='d' >>> id(s) 61711680
In Python, if you need to compare two objects to see if they are the same object (and you probably shouldn't), use is , which is the equivalent of the Python object.ReferenceEquals . id guaranteed only by a constant throughout the life of the object. As shown above, id not guaranteed to be different for different objects. In particular, if one of the objects no longer exists, you can (and sometimes) get the same id due to reusing the same memory. So using id is a really bad idea.
Summary
Do not do this.