gpt4 book ai didi

c# - 如何将 nullable int 转换为 nullable short?

转载 作者:行者123 更新时间:2023-11-30 13:22:20 25 4
gpt4 key购买 nike

我在寻找答案时得到的结果是指从 shortint 以及从可空到不可空的转换。但是,我无法理解如何将“较大”类型 int? 转换为“较小”类型 short?

我能想到的唯一方法就是写一个这样的方法:

private short? GetShortyOrNada(int? input)
{
if(input == null)
return (short?)null;
return Convert.ToInt16(input.Value);
}

不过,我希望在一行中完成,因为它只在项目的整个代码库中的一个地方完成,并且永远不会发生变化。

最佳答案

简单转换有什么问题?我测试过,效果很好。

private static short? GetShortyOrNada(int? input)
{
checked//For overflow checking
{
return (short?) input;
}
}

关于c# - 如何将 nullable int 转换为 nullable short?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25525874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com