Reputation: 1543
I'm working with Visual Studio 2019 and it suggested a conversion for a foreach to Linq which then doesn't compile:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
static void Main(string[] args)
{
var list = new List<object>();
list.Add("One");
list.Add("Two");
list.Add("Three");
// To make the point...
var iList = (IList)list;
// This doesn't compile and generates CS1061: IList does not contain a definition for Select.
// I thought Linq gave Select to list types?
var toStringList = iList.Select(s => s.ToString());
// The original foreach version:
var outputList = new List<string>();
foreach (var item in iList)
{
var itemToString = item.ToString();
outputList.Add(itemToString);
}
}
Upvotes: 0
Views: 964
Reputation: 151674
Linq is a set of extension methods on everything that implements IEnumerable<T>
.
The non-generic IList
doesn't implement that interface, so Linq won't work on it.
Visual Studio 2019 Professional with ReSharper doesn't suggest Select
for me:
Upvotes: 2