开发者

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

开发者 https://www.devze.com 2023-02-08 03:34 出处:网络
I want to do this: List<ushort> uList = new List<ushort>() { 1, 2, 3 }; List<short> sList = uList.Cast<short>().ToList();

I want to do this:

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

bu开发者_JAVA技巧t I get InvalidCastException "Specified cast is not valid."

How can I cast fast and efficient the above collection?


You could use ConvertAll:

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


List<short> sList = uList.Select(i => (short)i).ToList();
0

精彩评论

暂无评论...
验证码 换一张
取 消