views:

212

answers:

8

Is there a simple way of doing the following:

String s = myObj == null ? "" : myObj.ToString();

I know I can do the following, but I really consider it as a hack:

String s = "" + myObj;

It would be great if Convert.ToString() had a proper overload for this.

+8  A: 
string.Format("{0}", myObj);

string.Format will format null as an empty string and call ToString() on non-null objects. As I understand it, this is what you were looking for.

Holstebroe
+2  A: 

actually I didnt understand what do you want to do. As I understand, you can write this code another way like this. Are you asking this or not? Can you explain more?

string s = string.Empty;
    if(!string.IsNullOrEmpty(myObj))
    {
    s = myObj.ToString();
    }
Serkan Hekimoglu
Sure I can write it like this but I want a simple small function call, I was wondering why there is nothing like this in the .NET BCL
codymanix
Do you really need a function while we having IsNullOrEmpty() method?
Serkan Hekimoglu
On that note, I have always wanted the coalescing operator to fail next once it hits a null reference in your fluent expression, but I completely understand why that is a terrible idea. An override that allows you to do that would be nice too.
NickLarsen
`string s = string.IsNullOrEmpty(myObj) ? string.Empty : myObj.ToString();`
NickLarsen
+4  A: 

With an extension method, you can accomplish this:

public static class Extension
{
    public static string ToStringOrEmpty(this Object value)
    {
        return value == null ? "" : value.ToString();
    }
}

The following would write nothing to the screen and would not thrown an exception:

        string value = null;

        Console.WriteLine(value.ToStringOrEmpty());
Pieter
Do extension method calls omit the usual check for nulls...?
Matti Virkkunen
Well, here, inside the extension method I check for null. The example runs without exception.
Pieter
I don't think there could be anything more "hack-ish" then this. You have created a situation where it is perfectly legal to call a function on a null object... This is better implemented as a non extension utility function.
NickLarsen
That's the beauty of extension methods. This is completely allowed, and exactly does what codymanix is requesting in the question.
Pieter
+1 The fact that extension methods _can_ work with null references doesn't make this _hackish_, [I think](http://codecrafter.blogspot.com/2008/07/c-extension-methods-and-null-references.html).
Jordão
This extension doesn't add anything that string.Format doesn't already do.
Holstebroe
It adds that you don't have to type "{0}" and that you can choose a method name that describes what you're doing. This is especially useful when you're doing this many times. You could even name the method ToStringOrEmptyWhenNull.
Pieter
If the OP thinks that `string s = "" + myObj;` is hackish, calling a function on a null object should fall into that same boat. I would downvote this, but it does solve the problem at hand, I just disagree with the usage. Null objects should throw `NullReferenceException`, even in extension methods.
NickLarsen
Also, this should take object as its parameter, not string.
NickLarsen
Ah, yes, but they don't. Maybe we should Anders Hejlsberg why it doesn't throw?
Pieter
@NickLarsen: I agree with you on most occasions, but sometimes you just want the convenience of a simple method call. The method name should give you a hint that null references are OK, like with the proposal for `ToStringOrEmptyWhenNull`.
Jordão
Absolutely right about the object as parameter. Updated the answer.
Pieter
I was going to suggest something similar.
jimplode
@codymanix - Extension methods do not throw on null. Run the fragment in Visual Studio and you'll see that it does what you want.
Pieter
Extension methods do not throw when the "first" parameter (the `this` parameter) because they are just syntactic sugar. That is `x.SomeExtensionMethod()` is syntactic sugar for `SomeStaticClass.SomeExtensionMethod(x);` Thus, when `x` is `null` we are not attempting to invoke a method on a `null` object but rather passing a `null` object to a static method. If and only if that method checks for a `null` parameter and throws when encountering such will an extension method "invoked" on a `null` object throw.
Jason
+10  A: 
String s = (myObj ?? String.Empty).ToString();

or

String s = (myObjc ?? "").ToString()

to be even more concise.

Edit: As has been pointed out you'll often need a cast on either side to make this work with non String or Object types:

String s = (myObjc ?? (Object)"").ToString()
String s = ((Object)myObjc ?? "").ToString()
ach
Will this compile? Doesn't the coalescing operator check types?
NickLarsen
It works since (object??string) returns object, because coalesce uses the least common type. But I think this wont work for interfaces because it cannot decide which interface to chose (since multiple interfaces are allowed per class).
codymanix
You're right, it needs an object cast on one side in some cases...
ach
Not really the solution I was hoping for but I will accept it
codymanix
+3  A: 

I disagree with that this:

String s = myObj == null ? "" : myObj.ToString();

is a hack in any way. I think it's a good example of clear code. It's absolutely obvious what you want to achieve and that you're expecting null.

UPDATE:

I see now that you were not saying that this was a hack. But it's implied in the question that you think this way is not the way to go. In my mind it's definitely the clearest solution.

steinar
He didn't say that this method was hack. In fact he didn't say what was wrong with it except perhaps implying that it wasn't simple. I don't think there is anything wrong with this method. +1 because I would do it this way.
Mark Byers
Thanks - updated to clarify.
steinar
I agree with OP... there's already a null coalescing operator in c# - see my post below.
ach
I think the null coalescing operator is a little bit less clear in this case, however I would be just as fine with using that.
steinar
-1 because it does not answer the question.
Pieter
@Pieter Fair enough. But it does. The question is "Is there a simple way of doing the following:...". That exact way is simple!
steinar
I never said, that *this* method is a hack. Please reread my question. I only miss a little function in the Framework which has this functionality.
codymanix
@steinar ... this case IS the reason why null-coalesce exists.
ach
@ach I know. I mean that I don't really like as much when it's used in the parameters being sent down to the function as when assigned. I.e. f(something ?? "") versus s = something ?? "". That's just my taste.
steinar
@codymanix I had already updated the answer to reflect this.
steinar
@codymanix that's why I though the extension method ToStringOrEmpty was a nice idea :).
Pieter
+1  A: 

Holstebroe's comment would be your best answer:

string s = string.Format("{0}", myObj);

If myObj is null, Format places an Empty String value there.

It also satisfies your one line requirement and is easy to read.

jp2code
A: 

I might get beat up for my answer but here goes anyway:

I would simply write

string s = "" if (myObj != null) { x = myObj.toString(); }

Is there a payoff in terms of performance for using the ternary operator? I don't know off the top of my head.

And clearly, as someone above mentioned, you can put this behavior into a method such as safeString(myObj) that allows for reuse.

jaydel
OMG if object is null then call ToString() on it?
codymanix
Come on, that's a typo. I've replaced the first = with ! to make it correct. But you're really going to -1 me for a typo?
jaydel
+1  A: 
string s = String.Concat(myObj);

would be the shortest way I guess and also have neglible performance overhead. Keep in mind though it wouldn't be quite clear for the reader of the code what the intention is.

herzmeister der welten
This is the explicit form of ""+myObj but even worse in telling what is going on here. Interesting anyway.
codymanix