我的C#类中有一个IntPtr字段.
它包含对C++库中对象的引用.
protected IntPtr ThingPtr;
Run Code Online (Sandbox Code Playgroud)
在某些阶段,我可能会或可能不会初始化它.
ThingPtr = FunctionInMyCplusplusLibrary();
Run Code Online (Sandbox Code Playgroud)
我想知道在这种情况下检查是否为null是有意义的(检查它是否已被初始化)
if(ThingPtr == null)
{
//Do stuff
}
Run Code Online (Sandbox Code Playgroud)
SLa*_*aks 34
IntPtr 是值类型,不能为null.
您想检查它的值是否为(地址)0:
if (ThingPtr == IntPtr.Zero)
Run Code Online (Sandbox Code Playgroud)