为什么这个变量在c#中通过Mono被列为未赋值的局部变量?

本文关键字:赋值 局部变量 Mono 变量 为什么 | 更新日期: 2023-09-27 18:15:46

我无法确定为什么这里的代码不会在使用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) {

为什么这个变量在c#中通过Mono被列为未赋值的局部变量?

因为它是未分配的;您需要实例化一个hashset并将其分配给变量,或者至少分配给它null

正确。你需要初始化它