6

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?

2
  • Don't think there is anything shorter actually Commented Jul 13, 2012 at 14:14
  • Other than an extension method, don't see this getting any shorter. Commented Jul 13, 2012 at 14:17

5 Answers 5

8

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();
}
Sign up to request clarification or add additional context in comments.

4 Comments

I'm sorry about adding a similar answer, it's just that I thought of a more generic version than yours....
No sense keeping my answer as yours is effectively the same in a much cleaner extension method +1.
@Clodoaldo actually he is not :)
OK, I'll +1 you so that I don't feel so bad for being selected... Even if I get unselected that's fine...
5

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();

2 Comments

Oops.. This time I'm sorry - just wanted to create more generic version of my answer :) I didn't copy yours!
I need formatProvider parameter for decimal type so removed generic version to specialize to decimal extension as my answer in stackoverflow.com/a/47835010/1830909, Unfortunately can't write method in comment and must be written as new answer, do you have any better idea?
2

I think

string s = i?.ToString();

is shorter.

2 Comments

Yes! +1. But you have to appreciate that this question was asked and answered before that operator was available in the language. Nevertheless, I've updated my answer with that as well.
Yeah, was aware that the question predates the availability of the operator. Just thought I'd throw it out there in case it's helpful to anyone.
0

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();

Comments

-1

I need formatProvider parameter for decimal type so removed generic version to specialize to decimal extension as below:

public static string ToStringOrNull(this Nullable<decimal> nullable, string format = null)        
{
    string resTmp = "";
    if (nullable.HasValue)
    {
        if (format != null)
        {
            resTmp = nullable.Value.ToString(format);
        }
        else
        {
            resTmp = nullable.ToString();
        }
    }
    else
    {
        resTmp = "";
    }
    return resTmp;
}

1 Comment

@Jordão Yeah, I have some nullable decimal field in my db tables therefor in entity framework model defined as nullable decimal. For using them in web app view, must be convert to string and as nullable type I can't using the standard ToString() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.