List of C # castings to <ushort> to <short> list

I want to do it

List<ushort> uList = new List<ushort>() { 1, 2, 3 };
List<short> sList = uList.Cast<short>().ToList();

but I get InvalidCastException "The specified order is not valid."

How can I quickly and efficiently use the above collection?

Thank.

+5
source share
2 answers
List<short> sList = uList.Select(i => (short)i).ToList();
+7
source

You can use ConvertAll:

List<short> sList = uList.ConvertAll(x => (short)x);
+9
source

All Articles