开发者

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

开发者 https://www.devze.com 2023-02-07 04:18 出处:网络
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.

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.


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 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消