Convert nullable numeric into string
c#, type-conversion
Solution
You could create an extension method for that:
public static string ToStringOrNull<T>(this Nullable<T> nullable)
where T : struct {
return nullable.HasValue ? nullable.ToString() : null;
}
Usage:
var s = i.ToStringOrNull();
UPDATE
Since C# 6, you can use the much more convenient null-conditional operator:
var s = i?.ToString();
Problem
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?