Sander Koldenhof
Sander Koldenhof

Reputation: 1293

(C#) Type or namespace list could not be found while using system.Collections.generic

I have an issue with creating lists in C#. It says that the type or namespace "file<>" could not be found. I am using System.Collections.Generic and creating the list "correct" as far as I know.

I have basically nothing yet besides the list (worked with array earlier but it didn't meet the requirements):

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();
        }
    }
}

From what I know, you create a list by typing "list [name of the list] = new list();". However, this doesn't work. Why am I receiving this error message?

Upvotes: 2

Views: 3659

Answers (2)

Gaurang Dave
Gaurang Dave

Reputation: 4046

You need to make list to static.

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

Upvotes: 0

Ron Beyer
Ron Beyer

Reputation: 11273

C# is a case-sensitive language, so you need to watch casing. Also, Main in your program is a static method, and can only access static members of the Program class. So you need to mark the List<T> as static:

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();
        }
    }
}

Upvotes: 1

Related Questions