Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Casting List<ushort> to List<short>

I want to do this:

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

but I get InvalidCastException "Specified cast is not valid."

How can I cast fast and efficient the above collection?

like image 902
Tomislav Markovski Avatar asked Feb 03 '11 16:02

Tomislav Markovski


2 Answers

You could use ConvertAll:

List<short> sList = uList.ConvertAll(x => (short)x);
like image 115
Eric Petroelje Avatar answered Oct 19 '22 19:10

Eric Petroelje


List<short> sList = uList.Select(i => (short)i).ToList();
like image 26
Rover Avatar answered Oct 19 '22 18:10

Rover