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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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.

You Might Also Like