C#的規範是否阻止從對象的(或結構)初始值設定項構造中調用方法?對象初始化程序中的函數和屬性用法
我問的原因是因爲我試圖使用LINQ-to-XML語句在初始化程序中使用gater數據。這不起作用。但是,如果我在將數據保存到本地變量之前獲取數據,那麼它的工作沒有問題。我只是想知道爲什麼會發生這種情況,因爲我已經找到了我的代碼中的錯誤。
不起作用:
SavedData sData = new SavedData()
{
exportLocation = data.Root.Descendants("ExportLocation").FirstOrDefault().Value,
exportType = (ExportType)data.Root.Descendants("ExportType").FirstOrDefault().Value
};
作品:
var exLoc = data.Root.Descendants("ExportLocation").FirstOrDefault().Value;
ExportType type = (ExportType)data.Root.Descendants("ExportType").FirstOrDefault().Value;
Saved Data sData = new SavedData()
{
exportLocation = exLoc,
exportType = type
};
*你的意思是什麼*不起作用*?它編譯對我來說很好。 – MarcinJuraszek
它應該工作。如果你告訴我們它不完全正常工作,我們也許可以幫忙。 – fejesjoco
@MarcinJuraszek _在這種情況下不工作_並不意味着它不能編譯。該對象不會初始化並等於null。 – JNYRanger