Why does the following not succeed in castingIEnumerable<string> to List<string>?
var l = new List<Tuple<string, int>>();
l.Add(new Tuple<string, int>("a string", 1));
List<string> s = (List<string>)l.Select(x => x.Item1); // System.InvalidCastException
MessageBox.Show(s[0]);
Also, why is the exception not caught properly in Visual Studio? It appears in the debug window but doesn’t stop execution of the program.
Selectreturns anIEnumerable<T>. If you want the results as aList<T>, use:Internally the
Selectmethod is not generating aListat all; the elements are returned on the fly as theIEnumerable<T>is iterated.I would except the Exception to be caught. My guess would be that you have a catch (perhaps not one that you added) that is picking it up somewhere along the way.