简体   繁体   中英

Using the generic type 'System.Collections.Generic.List<T>' requires '1' type arguments

I am trying to create a program that will create a vector, generate 100 random numbers (0 - 99) and then ask for user input whether they want the numbers sorted from High to Low or Low to High.

This is the code I have so far just trying to get the vector working.

using System;
using System.Collections.Generic;
using System.Collections.Generic.List; //this is where the error comes.
using System.Linq;
using System.Text;

namespace System.Collections.Generic
{
    class List<T>
    {
        static void Main(string[] args)
        {
            List<int> vectorList = new List<int>();
            vectorList.Capacity(100);
            int i = 100;
            for (int x = 0; x <= 100; x++)
            {
                Random rand = new Random();
                i = rand.Next(0, 100);
                vectorList.Add(i);
                Console.WriteLine(i);
            }
        }
    }
}

Except I have no idea how to fix this issue, any help would be much appreciated.

Remove that using line; List is a class, not a namespace so you don't have to "use" it.

Your Main() method ought to belong in its own class too; C# does not allow redefining an existing class to add a Main() method.

Also, Joel Coehoorn's answer will solve the problem you're having that I believe made you put that using line there in the first place, and LBushkin's advice is worth heeding.

It's a side issue (BoltClock has it right), but change this:

List<int> vectorList = new List<int>();
vectorList.Capacity(100);

To this:

List<int> vectorList = new List<int>(100);

Also, remove the <T> from your class name.

There are a number of things you're doing here that aren't a good idea, and are directly responsible for some of the errors you're going to get.

First, in C#, the using directive is intended to identify an entire namespace that you will depend on - and all of the types exposed in that namespace. The line

using System.Collections.Generic.List

doesn't refer to a namespace, but to a type ( List<T> ) - and is not appropriate.

Next, you are trying to create a generic class List<T> as the main class of your program ... and in the System.Collections.Generic namespace no less. Well, this will conflict with the existing .NET List<T> class - so you will get errors from that as well.

What you really want to do here is create a namespace that is associated with (or describes) the program you are writing. Don't try to re-use the existing .NET framework namepsaces for that purpose. And don't name your class List<T> .

Finally, your class is not really a generic class ... the type parameter T is not used for any purpose in the program code. And your class is not an implementation of a list ... it's a ListProcessor (or something along those lines). You should be careful when naming classes or interfaces in a program so that they have a strong relationship to what they are named. It's also a good idea (when possible) to avoid creating names that collide with those already used by common classes in the .NET framework - as that can cause ambiguity and confusion in your code.

remove:

using System.Collections.Generic.List;

List is contained in the Generic namespace which you are already "using".

You only need to import the namespace. So remove the offending line.

Other than that, you have declared your program class to be generic List<T> . You don't need that, your entry point should just be in a normal static class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM