【发布时间】:2012-03-25 20:01:57
【问题描述】:
我刚刚在Debug 模式(VS 2008 Express,Any Cpu)下运行应用程序时遇到了一些非常奇怪的事情。如果有人启发我了解这里发生了什么,我将不胜感激?
// PredefinedSizeGroupMappings is null here
Dictionary<string, int> groupIDs = PredefinedSizeGroupMappings ?? new Dictionary<string, int>();
// so groupIDs is now initialized as an empty Dictionary<string, int>, as expected
// now: PredefinedSizesMappings is null here - therefore I expect sizeIds
// to be initialized as an empty dictionary:
Dictionary<string, string> sizeIds = PredefinedSizesMappings ?? new Dictionary<string, string>();
// but at this point sizeIds is still null! :O That's what debugger shows.
var groupsReport = new AutomappingReportArgs();
// only once we get here - it's suddenly not... The debugger shows: "Count = 0"
var sizesReport = new AutomappingReportArgs();
AutomappingReportArgs 类与 sizeIds 变量没有任何联系,尽管它的构造函数确实分配了许多字典:
public AutomappingReportArgs()
{
ChangedNames = new Dictionary<string, KeyValuePair<string, string>>();
CreatedAfterRename = new Dictionary<string, string>();
Existing = new Dictionary<string, string>();
Created = new Dictionary<string, string>();
Failed = new Dictionary<string, string>();
}
我猜它一定是某种编译器或 CLR 优化,但我想更详细地了解它的机制。这种“延迟初始化”的原因是什么?
为什么它不一致,为什么它对Dictionary<string, int> 立即起作用,但对Dictionary<string, string> 不起作用?是不是因为编译器前面看不到任何Dictionary<string, int>初始化,所以不能放在一边待会儿?
【问题讨论】:
-
您是否清理过构建并重试?
-
我重复了几次。在什么意义上清理它?
-
像右键单击解决方案然后单击“清除解决方案”一样清洁。
-
VS 2008 Express 中没有这样的东西
-
那么应该有一个重建。
标签: c# debugging .net-3.5 clr jit