简体   繁体   中英

C# Can I check if an IntPtr is null?

I have an IntPtr field in my C# class.
It holds a reference to an object in a C++ library.

protected IntPtr ThingPtr;

At some stage I may or may not initialise it.

ThingPtr = FunctionInMyCplusplusLibrary();

I'm wondering if checking whether it is null makes sense in this context (to check whether it has been intialised or not)

if(ThingPtr == null)
{
    //Do stuff
}

IntPtr is a value type and cannot be null.

You want to check whether it has a value of (address) 0:

if (ThingPtr == IntPtr.Zero)

IntPtr是一个永远不能为null的结构,你的库可能返回null的等价物,但我希望它是零。

您可以将IntPtr.Zero用于null,但它不等同于C#null值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM