Cannot implicity convert type 'int' to 'ushort' : already explicity cast

馋奶兔 提交于 2019-12-04 07:35:11

(ushort) channel is ushort but 12 * (ushort)(channel) would be int, do this instead:

ushort quotient = (ushort) ((12 * channel) / 16);

Multiplication of any int and smaller types produces int. So in your case 12 * ushort produces int.

ushort quotient = (ushort)(12 * channel / 16);

Note that above code is not exactly equivalent to original sample - the cast of channel to ushort may significantly change result if value of channel is outside of ushort range (0.. 0xFFFF). In case if it is important you still need inner cast. Sample below will produce 0 for channel=0x10000 (which is what original sample in question does) unlike more regular looking code above (which gives 49152 result):

ushort quotient = (ushort)((12 * (ushort)channel) / 16); 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!