且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用未分配的局部变量'字典'

更新时间:2022-06-22 00:20:34

您需要在这里明确:

Dictionary<String, String> dictionary = null;

有可能性在尝试使用时不会分配在第二个尝试语句中,例如,如果在第一次尝试中立即抛出异常,则该字典不会指向任何内容。这不会阻止一个 null 引用异常(你将不得不处理),它只是让你的意图清楚了编译器。

There's the possibility it won't be assigned when you try to use it in your second try statement, for example if you throw an exception immediately in the first try, the dictionary wouldn't point to anything. This won't prevent a null reference exception (you'll have to handle that), it just makes your intent clear to the compiler.