我會生氣嗎?爲什麼這會返回一個異常!?爲什麼找不到鑰匙?顯然是在那裏!C#字典找不到字符串鍵
public class Spanning {
Dictionary<string, City> graph = new Dictionary<string, City>();
public static void Main(string[] args) {
new Spanning().Parse("file.in");
}
public void Parse(string f) {
string[] citySeparator = new string[] {"--"};
using (StreamReader reader = new StreamReader(f)) {
string line;
while((line = reader.ReadLine()) != null) {
if (!line.Contains("--")) {
graph[line] = new City {name = line.Substring(0, line.Length - 1)};
} else {
string[] split = line.Split(citySeparator, StringSplitOptions.None);
City city1 = graph[split[0]];
}
}
}
}
class City {
Dictionary<City, int> links = new Dictionary<City, int>();
public string name;
public override string ToString() {
return name;
}
}
}
輸入文件只包含兩行:
"San Diego"
"San Diego"--"San Antonio" [1319]
你什麼異常? –
你可以通過代碼? –
嘗試放置一個斷點並逐步查看正在發生的事情。 – Habib