C# new IntPtr(0) 与 IntPtr.Zero

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/560244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:11:16  来源:igfitidea点击:

new IntPtr(0) vs. IntPtr.Zero

c#.netpinvoke

提问by Yuval Peled

Is there any difference between the two statements:

这两种说法有什么区别吗:

IntPtr myPtr = new IntPtr(0);
IntPtr myPtr2 = IntPtr.Zero;

I have seen many samples that use PInvoke that prefer the first syntax if the myPtr argument is sent by ref to the called function. If I'll replace all new IntPtr(0) with IntPtr.Zero in my application, will it cause any damage?

我已经看到许多使用 PInvoke 的示例,如果 myPtr 参数由 ref 发送到被调用函数,则它们更喜欢第一种语法。如果我将应用程序中所有新的 IntPtr(0) 替换为 IntPtr.Zero,是否会造成任何损坏?

采纳答案by Keith

IntPtris a value type, so unlike String.Emptythere's relatively little benefit in having the static property IntPtr.Zero

IntPtr是一种值类型,因此与String.Empty静态属性的好处相对较小IntPtr.Zero

As soon as you pass IntPtr.Zeroanywhere you'll get a copy, so for variable initialisation it makes no difference:

只要你通过IntPtr.Zero任何地方,你就会得到一个副本,所以对于变量初始化,它没有区别:

IntPtr myPtr = new IntPtr(0);
IntPtr myPtr2 = IntPtr.Zero;

//using myPtr or myPtr2 makes no difference
//you can pass myPtr2 by ref, it's now a copy

There is one exception, and that's comparison:

有一个例外,那就是比较:

if( myPtr != new IntPtr(0) ) {
    //new pointer initialised to check
}

if( myPtr != IntPtr.Zero ) {
    //no new pointer needed
}

As a couple of posters have already said.

正如一些海报已经说过的那样。

回答by Richard Szalay

They are functionally equivalent, so it should cause no problems.

它们在功能上是等效的,因此应该不会引起任何问题。

IntPtr.Zerorepresents the default state of the structure (it is declared but no constructor is used), so the default value of the intptr (void*)would be null. However, as (void*)nulland (void*)0are equivalent, IntPtr.Zero == new IntPtr(0)

IntPtr.Zero表示结构的默认状态(已声明但未使用构造函数),因此 的默认值intptr (void*)将是null。然而,由于(void*)null(void*)0是等价的,IntPtr.Zero == new IntPtr(0)

Edit:While they are equivalent, I do recommend using IntPtr.Zerofor comparisons since it simply is easier to read.

编辑:虽然它们是等效的,但我确实建议将其IntPtr.Zero用于比较,因为它更易于阅读。

回答by Anton Gogolev

It's mostly a matter encapsulation (and of performance, but to a much lesser extent). At some moment in the future Microsoft may decide (though it's very unlikely) that an unitialized pointer value will from now on equal to 0xDEADBEEF, thus rendering all new IntPtr(0)code invalid.

这主要是一个问题封装(和性能,但程度要小得多)。在未来的某个时刻,Microsoft 可能会决定(尽管可能性很小)一个未初始化的指针值将从现在开始等于0xDEADBEEF,从而使所有new IntPtr(0)代码无效。

As far as performance is concerned, MSDN says this:

就性能而言,MSDN 是这样说的:

For example, assume the variable, ip, is an instance of IntPtr. You can determine if it has been set by comparing it to the value returned by a constructor, for example: " if ip != new IntPtr(0)... ". However, invoking a constructor to get an unintialized pointer is inefficient. It is better to code either " if ip != IntPtr.Zero...", or " if !IntPtr.Zero.Equals(ip)...".

例如,假设变量ip是 的一个实例IntPtr。您可以通过将其与构造函数返回的值进行比较来确定它是否已设置,例如:“if ip != new IntPtr(0)...”。但是,调用构造函数来获取未初始化的指针是低效的。最好编码“ if ip != IntPtr.Zero...”或“ if !IntPtr.Zero.Equals(ip)...”。

回答by bruno conde

The use of IntPtr.Zerowill allow you to avoid a new instance of IntPtr.

的使用IntPtr.Zero将允许您避免IntPtr.

from msdn:

来自msdn

Use this field to efficiently determine whether an instance of IntPtr has been set to a value other than zero

使用此字段可有效确定 IntPtr 的实例是否已设置为零以外的值

回答by Drew Noakes

What happens if you pass IntPtr.Zeroby ref, and the recipient tries to modify the reference? From that moment forth, would IntPtr.Zero != new IntPtr(0), or would the recipient receive some kind of exception upon trying to make the change?

如果你通过IntPtr.Zeroref,而接收者试图修改引用,会发生什么?从那一刻起,IntPtr.Zero != new IntPtr(0)在尝试进行更改时,接收方是否会收到某种异常?

I'm not sure about this, but it seems like a reasonable explanation.

我不确定这一点,但这似乎是一个合理的解释。

回答by Joshua A. Schaeffer

The JITter can inline IntPtr.Zero the same way it inlines IntPtr.Size.

JITter 可以像内联 IntPtr.Size 一样内联 IntPtr.Zero。