我在使用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;来创建列表。但是,这不起作用。为什么我收到此错误消息?
答案 0 :(得分:1)
C#是一种区分大小写的语言,因此您需要观看套管。此外,您的计划中的Main
是static
方法,并且只能访问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>();