(C#)使用system.Collections.generic

时间:2018-03-02 01:52:11

标签: c# list types namespaces

我在使用C#创建列表时遇到问题。它表示类型或命名空间"文件<>"无法找到。我正在使用System.Collections.Generic并创建列表"更正"据我所知。

除了列表之外我基本上什么都没有(早先使用数组,但它没有达到要求):

using System;
using System.IO;
using System.Collections.Generic;

namespace Week_3_Opdracht_3
{
    class Program
    {
        public list<string> streamReaderResults = new list<string>();


        private static string currentFileLine;

        static void Main(string[] args)
        {

            StreamReader opdrachtDrieFile = new StreamReader(@"C:\Users\sande\Documents\VisualStudio school\.txt files to read\Opdracht 3 tekst.txt");

            while ((currentFileLine = opdrachtDrieFile.ReadLine()) != null)
            {
                //nothing yet.
            }
            Console.ReadKey();
        }
    }
}

据我所知,您可以通过输入&#34; list [列表名称] =新列表();&#34;来创建列表。但是,这不起作用。为什么我收到此错误消息?

2 个答案:

答案 0 :(得分:1)

C#是一种区分大小写的语言,因此您需要观看套管。此外,您的计划中的Mainstatic方法,并且只能访问static类的Program个成员。因此,您需要将List<T>标记为静态:

namespace Week_3_Opdracht_3
{
    class Program
    {
        public static List<string> streamReaderResults = new List<string>();


        private static string currentFileLine;

        static void Main(string[] args)
        {

            StreamReader opdrachtDrieFile = new StreamReader(@"C:\Users\sande\Documents\VisualStudio school\.txt files to read\Opdracht 3 tekst.txt");

            while ((currentFileLine = opdrachtDrieFile.ReadLine()) != null)
            {
                //nothing yet.
            }
            Console.ReadKey();
        }
    }
}

答案 1 :(得分:0)

您需要将列表设为静态。

public static List<string> streamReaderResults = new List<string>();