我无法确定为什么这里的代码不能使用Mono 2.10.6在Win32上的MonoDevelop 2.8.2中编译。 Monodevelop表明found_image_paths是一个未分配的局部变量?
我在这里遗漏了什么吗?我是C#
的新手 string path = this.DirectoryChooser.CurrentFolder;
Console.WriteLine ("Selected Path: " + path);
//Iterate over all DAE files in this folder
string[] model_paths = Directory.GetFiles(path,"*.dae");
HashSet<string> found_image_paths;
foreach (string dae_path in model_paths)
{
XmlDocument xmlDoc= new XmlDocument(); //* create an xml document object.
xmlDoc.Load(dae_path); //* load the XML document from the specified file.
//* Get elements.
XmlNodeList library_images = xmlDoc.GetElementsByTagName("library_images");
System.Console.WriteLine(dae_path);
foreach (XmlNode image_node in library_images[0].ChildNodes) {
string image_path = image_node.FirstChild.InnerText;
found_image_paths.Add(image_path);
Console.WriteLine(image_path);
}
}
//The next line returns the error "Use of unassigned local variable 'found_image_paths'
foreach (var item in found_image_paths) {
答案 0 :(得分:4)
因为它未分配;您需要实例化一个哈希集并将其分配给您的变量,或者至少为其分配null
。
答案 1 :(得分:2)
这是对的。你需要初始化它。