Convert nullable numeric into string

Viewed 5112

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?

5 Answers

We can also use System.Convert i.e.

int? i = null;
string s = Convert.ToString(i); // it gives null if i is null.

And as already mentioned we can also use null conditional operator

string s = i?.ToString();
Related