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...
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?
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());
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());
}