Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 834
Cistoran Avatar asked Jan 25 '11 19:01

Cistoran


2 Answers

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.

like image 123
Joel Coehoorn Avatar answered Oct 13 '22 01:10

Joel Coehoorn


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.

like image 32
LBushkin Avatar answered Oct 13 '22 00:10

LBushkin