I want to convert a nullable numeric into a string maintaining the null value. This is what I'm doing:
int? i = null;
string s = i == null ? null : i.ToString();
Is there something shorter?
You can write some extension method:
public static string ToNullString(this int? i)
{
return i.HasValue ? i.ToString() : null;
}
Usage will be more simple:
string s = i.ToNullString();
Or generic version:
public static string ToNullString<T>(this Nullable<T> value)
where T : struct
{
if (value == null)
return null;
return value.ToString();
}
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