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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With