C # Is it possible to check if IntPtr is null?

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

protected IntPtr ThingPtr; 

At some point, I may or may not initialize it.

 ThingPtr = FunctionInMyCplusplusLibrary(); 

I am wondering if it checks for null in this context (to check if this was intended or not)

 if(ThingPtr == null) { //Do stuff } 
+6
source share
3 answers

IntPtr is a value type and cannot be null.

You want to check if the value (address) is 0:

 if (ThingPtr == IntPtr.Zero) 
+25
source

IntPtr is a structure that can never be null, your library can return the null equivalent, but I expect it to be null.

+3
source

You can use IntPtr.Zero for null, however this is not equivalent to a C # null value.

+3
source

Source: https://habr.com/ru/post/975543/


All Articles