C# 相当于 Python 的 id() 吗?
是否有 C# 相当于 Python 的 id() ?如果没有,我如何测试对象是否已更改?例如,在 Python 中,一个不可变对象:
>>> s = "abc"
>>> id(s)
11172320
>>> s += "d"
>>> id(s)
18632928
和一个可变对象:
>>> a = [1, 2, 3]
>>> id(a)
11259656
>>> a += [4]
>>> id(a)
11259656
编辑 - 到目前为止,大多数答案都是关于比较函数/运算符的,但我对正在比较的值更感兴趣。我最初并不清楚这一点,我接受 Object.ReferenceEquals
作为答案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
Object.ReferenceEquals
。You can test object identity with
Object.ReferenceEquals
.那么,您可以使用
Object.ReferenceEquals()< /代码>
功能:
借用一下你的 Python 示例:
equal
将是true
。Well, you can use the
Object.ReferenceEquals()
function:To borrow your Python example a little bit:
equal
will betrue
.我认为你问错了问题。我的回答非常详细,但这里有一个快速总结:
id
直接等效的东西。object.ReferenceEquals
执行与您尝试执行的操作类似的操作。首先,我将从 id 文档 发布,因为大多数人不这样做好像没读过。请一定阅读本文,因为只有当您知道
id
的作用时,我的其余答案才有意义(不,它不会创建哈希码):C# 没有与
id
等效的东西,但可以通过在不安全模式下获取指向对象的指针来获取对象的地址。但是,您不应该在 C# 中执行此操作。您很少需要在 C# 中使用不安全代码,如果您这样做,它很容易传播,因为调用您代码的代码也变得不安全。还有其他方法可以解决您的问题,不需要不安全的代码。此外,在 .NET 中(与 Python 不同),垃圾收集器可以在内存中移动对象,因此,如果您想要一个在对象的整个生命周期内保持不变的单一标识,那么内存地址对您来说没有用处。您应该创建一个只读属性,例如名为
Id
的属性。在 C# 中无需使用不安全代码即可实现的另一种方法是使用
Object.ReferenceEquals
。但是您可能也不应该这样做。 C# 不像 Python 那样具有歧义性。要将变量设置为指向新对象,请使用=
运算符。调用方法几乎总是不会更改引用,因此您不必担心。在 C# 中,x += y;
运算符(非常接近)等同于x = x + y
,并且您无法像在 Python 中那样更改它。另外,正如文档中提到的,您的 Python 示例存在缺陷 - 无法可靠地使用 id 来测试对象是否已更改。示例:
在 Python 中,如果您必须比较两个对象以查看它们是否是同一个对象(您可能不应该这样做),则使用
is
,它是 Python 中的object 等效项.ReferenceEquals
。id
仅保证在对象的生命周期内是常量。如上所示,id
对于不同的对象不保证是不同的。特别是如果其中一个对象不再存在,由于重复使用相同的内存,您可以(有时会)获得相同的id
。因此,使用 id 在这里是一个非常糟糕的主意。总结
不要这样做。
I think you're asking the wrong question. My answer is quite detailed, but here's a quick summary:
id
in C#.object.ReferenceEquals
does something similar to what you are trying to do.id
doesn't solve the problem in Python either.Firstly, I'll post from the documentation for id since most people don't seem to have read it. Please do read this because the rest of my answer only makes sense if you know what
id
does (and no, it doesn't create a hash code):C# doesn't have an equivalent to
id
but it is possible to get the address of the object by getting a pointer to the object in unsafe mode.However you shouldn't do this in C#. You very rarely need to use unsafe code in C#, and if you do it can easily spread as code that calls your code also becomes unsafe. There are other ways to solve your problem that don't need unsafe code. Besides, in .NET (unlike in Python) objects can be moved about in memory by the garbage collector, so if you want a single identity that remains constant for the entire lifetime of the object, the memory address is of no use to you. You should instead create a readonly property, called
Id
for example.An alternative approach which is possible in C# without using unsafe code is to test if two variables have the same reference using
Object.ReferenceEquals
. However you probably shouldn't do this either. C# doesn't have the same ambiguity as in Python. To set a variable to point to a new object you use the=
operator. Calling a method almost always won't change the reference, so you shouldn't worry about it. In C#x += y;
operator is (very nearly) equivalent tox = x + y
and you can't change that like you can in Python.Also, as mentioned in the documentation, your Python example is flawed -
id
cannot be used reliably to test if an object has changed. Example:In Python if you have to compare two objects to see if they are the same object (and you probably shouldn't be doing this) then use
is
which is the Python equivalent ofobject.ReferenceEquals
.id
is only guaranteed to be the constant over the lifetime of an object. As shown aboveid
is not guaranteed to be different for different objects. In particular if one of the objects no longer exists you can (and sometimes will) get the sameid
due to reuse of the same memory. As such the use ofid
is a really bad idea here.Summary
Don't do it.
object.GetHashcode()
可能值得一试。object.GetHashcode()
might be worth a shot.wes,你的问题的正确答案,即.. id() 的类似函数是
wes, the correct answer to your question, ie.. an analogous function to id() is