System. Collections. Generic. List Requires '1' Type Arguments

I have this error whith the following code:

string[] colors = { "green", "brown", "blue", "red" };
var list = new List(colors);
IEnumerable query = list.Where(c => c.length == 3);
list.Remove("red");
Console.WriteLine(query.Count());

Moreover, Count() does not seem to be allowed anymore. Is it deprecated?

2

4 Answers

You are trying to create a List<string> and you should tell that to the compiler

var list = new List<string>(colors);

There is no List, there is a generic class named List<T>, requires a type parameter.You can't create a generic list without specifying the type parameter.

Also you are trying to call Count extension method.That method takes IEnumerable<T> as first parameter,not IEnumerable, here is the definition:

public static int Count<TSource>(this IEnumerable<TSource> source)

so you should use IEnumerable<string> to access that extension method:

IEnumerable<string> query = list.Where(c => c.Length == 3);
list.Remove("red");
Console.WriteLine(query.Count());
0

You are using System.Collections.Generic.List<T>, so it is a generic list, therefore you must provide the generic parameter T. In your example case, you want a List<string>. So try:

List<string> list = new List<string>(colors);

Actually, you don't need a List at all in your sample and you can simplify everything a lot:

var count = new [] { "green", "brown", "blue", "red" }
    .Where(c => c.length == 3)
    .Where(c => c != "red")
    .Count();
Console.WriteLine(count);

Try this:

static void Main(string[] args)
{
    string[] colors = { "green", "brown", "blue", "red" };
    var list = new List<string>(colors); // <string>
    IEnumerable query = list.Where(c => c.Length == 3); // "Length", not "length"
    list.Remove("red");
    Console.WriteLine(query.Count());
}
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest