The hits I get when I look for the answer refer to casting from short to int and from nullable to not-nullable. However, I get stuck on how to convert from a "larger" type int? to a "smaller" type short?.
The only way I can think of is writing a method like this:
private short? GetShortyOrNada(int? input)
{
if(input == null)
return (short?)null;
return Convert.ToInt16(input.Value);
}
I wish to do that on a single line, though, because it's only done a single place in the entire code base for the project and won't ever be subject to change.
What's wrong with simple cast? I tested and that works fine.
private static short? GetShortyOrNada(int? input)
{
checked//For overflow checking
{
return (short?) input;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With