相当于托管c ++中的C#null

时间:2011-01-10 06:38:31

标签: c# managed-c++

我有一个托管C ++对象

bool Test::TestStringNumber(String^ testData)
{
   int len = testData->Length;
   if(len > 0) return true;
}

在C#中使用相同的功能

void Main()
{
    Test t = new Test()
    t.TestStringNumber(null);
}

但应用程序崩溃。在调试期间,我在C ++中观察到它是'未定义的值'

另外我试图用c ++代码中的nullptr测试testData,并使用下面的代码,但仍然崩溃

if( testData == nullptr && String::IsNullOrEmpty( testData )) 

*一种方法是在C ++托管代码中捕获异常并返回;

1 个答案:

答案 0 :(得分:6)

bool Test::TestStringNumber(String^ testData)
{
  return !String::IsNullOrEmpty( testData );
}

应该工作得很好吗?不是我在你的代码中看到了这一点吗?