C# IntPtr.Zero 等价于 null 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1456861/
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-06 17:43:56  来源:igfitidea点击:

Is IntPtr.Zero equivalent to null?

c#file-ioasynchronous

提问by SwDevMan81

I am trying to setup ReadFileto run asynchronously and according to MSDN, I need to set lpNumberOfBytesReadto null:

我正在尝试设置ReadFile为异步运行,根据MSDN,我需要设置lpNumberOfBytesReadnull

"Use NULL for this parameter if this is an asynchronous operation to avoid potentially erroneous results."

“如果这是一个异步操作,请为此参数使用 NULL 以避免潜在的错误结果。”

For example, if I have the following:

例如,如果我有以下内容:

  [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  public static extern bool ReadFile(
     IntPtr hFile,
     out byte[] aBuffer,
     int cbToRead,
     IntPtr cbThatWereRead,
     ref OVERLAPPED pOverlapped
  );

and I call it like this (with the intention of having the 4th parameter being null):

我这样称呼它(目的是让第 4 个参数为空):

Win32API.ReadFile(readHandle, out data_read, Win32API.BUFFER_SIZE, IntPtr.Zero, ref over_lapped);

will that be the same as calling it with null? If not, what should I change in the declaration or in the function call itself?

这和用null调用它一样吗?如果不是,我应该在声明或函数调用本身中更改什么?

I was also curious if I should be using SafeHandleor HandleRefinstead of IntPtrfor the hFilereference? I know to make sure that I close the handle with CloseHandle(IntPtr)when I'm done with it, just not sure if there is any othe reason to use the other two options over IntPtr. I am also tryingn to avoid using unsafe code.

我也好奇,如果我应该使用SafeHandleHandleRef代替IntPtrhFile参考?我知道要确保CloseHandle(IntPtr)在完成后关闭手柄,只是不确定是否有任何其他理由在IntPtr. 我也试图避免使用不安全的代码。

EDIT: As it turns out, I shouldnt be setting the fourth parameter to IntPtr.Zeroanyway, because even though I am running asynchronously, it could still return right away. See Asynchronous Disk I/O. Ahh, I love contradicting stories.

编辑:事实证明,IntPtr.Zero无论如何我都不应该将第四个参数设置为,因为即使我异步运行,它仍然可以立即返回。请参阅异步磁盘 I/O。啊,我喜欢矛盾的故事。

采纳答案by Michael

For P/Invoke purposes like you've listed, you should use IntPtr.Zeroin place of NULL. Note that this is not equivalent to the C# nullkeyword, however.

对于P / Invoke的目的,就像你上市,你应该使用IntPtr.Zero的地方NULL。但是请注意,这并不等同于 C#null关键字。

回答by Yannick Motton

You cannot assign null to a value-type. A reference-type can be null, as in, not referring to an object instance, but a value-type always has a value.

您不能将 null 分配给值类型。引用类型可以为空,例如,不引用对象实例,但值类型始终具有值。

IntPtr.Zero is just a constant value that represents a null pointer.

IntPtr.Zero 只是一个表示空指针的常量值。

回答by xanatos

Be aware that there is a bug (feature??) in C# >= 2.0, where

请注意,在 C# >= 2.0 中存在一个错误(功能??),其中

if (IntPtr.Zero == null)
{
    // Won't enter here
}

will compile correctly, but it won't ever enter in the if.

将正确编译,但它永远不会进入if.

I opened an issue on the github of roslynand they replied that they won't fix it because there are projects that are built with warnings-as-errors. Still there is a partial fix for this: there is a strictcompilation mode that generates this warning:

我在roslyngithub上打开了一个问题,他们回答说他们不会修复它,因为有些项目是用警告作为错误构建的。仍然有一个部分修复:有一个strict编译模式会产生这个警告:

<Features>strict</Features>