尽管我在以下代码中分配了值,但我仍收到错误 Use of unassigned local variable 'dictionary':
private static void UpdateJadProperties(Uri jadUri, Uri jarUri, Uri notifierUri)
{
Dictionary<String, String> dictionary;
try
{
String[] jadFileContent;
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(jadUri.AbsolutePath.ToString()))
{
Char[] delimiters = { '\r', '\n' };
jadFileContent = sr.ReadToEnd().Split(delimiters, System.StringSplitOptions.RemoveEmptyEntries);
}
// @@NOTE: Keys contain ": " suffix, values don't!
dictionary = jadFileContent.ToDictionary(x => x.Substring(0, x.IndexOf(':') + 2), x => x.Substring(x.IndexOf(':') + 2));
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
try
{
if (dictionary.ContainsKey("MIDlet-Jar-URL: "))
{
// Change the value by Remove follow by Add
}
}
catch (ArgumentNullException ane)
{
throw;
}
}
错误来自行:
if (dictionary.ContainsKey("MIDlet-Jar-URL: "))
有人可以帮我解决这个问题吗?时间差
最佳答案
你需要在这里明确:
Dictionary<String, String> dictionary = null;
当您尝试在第二个 try
语句中使用它时,可能它不会被分配,例如,如果您在第一次尝试中立即抛出异常,字典不会指向任何东西。这不会阻止 null
引用异常(你必须处理它),它只是让编译器清楚你的意图。
关于c# - 使用未分配的局部变量 'dictionary',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3066792/