我不明白爲什麼當傳遞變量在堆上時跟蹤引用沒有完成它的工作。下面是代碼:爲什麼跟蹤引用不會爲引用的對象分配值?
ref class DataContainer
{
public:
property DateTime Time;
};
DataContainer^ dc = gcnew DataContainer();
DateTime timeOnStack;
// first call with output variable on the stack
bool timeParsed = DateTime::TryParseExact ("20160104132500184", "yyyyMMddHHmmssfff", CultureInfo::InvariantCulture, DateTimeStyles::None, timeOnStack);
// second call with output variable on the heap
timeParsed = DateTime::TryParseExact ("20160104132500184", "yyyyMMddHHmmssfff", CultureInfo::InvariantCulture, DateTimeStyles::None, (dc->Time));
首先TryParseExact
調用,它使用本地初始化的變量timeOnStack
把解析的DateTime值按預期工作,並設置正確分析日期:致電timeOnStack.ToString()
回報"1/4/2016 1:25:00 PM"
而第二個甚至返回true
它不設置正確的值爲dc->Time
:致電dc->Time.ToString()
返回"1/1/0001 12:00:00 AM"
我在這裏沒有看到什麼?