Checking for null before ToString()
C#.NetStringPropertiesC# Problem Overview
Here's the scenario...
if (entry.Properties["something"].Value != null)
attribs.something = entry.Properties["something"].Value.ToString();
While effective and working correctly, this looks ugly to me. If I don't check for a null before performing the ToString() then it throws an exception if the property was null. Is there a better way to handle this scenario?
Much appreciated!
C# Solutions
Solution 1 - C#
Update 8 years later (wow!) to cover c# 6's null-conditional operator:
var value = maybeNull?.ToString() ?? String.Empty;
Other approaches:
object defaultValue = "default";
attribs.something = (entry.Properties["something"].Value ?? defaultValue).ToString()
I've also used this, which isn't terribly clever but convenient:
public static string ToSafeString(this object obj)
{
return (obj ?? string.Empty).ToString();
}
Solution 2 - C#
If you are targeting the .NET Framework 3.5, the most elegant solution would be an extension method in my opinion.
public static class ObjectExtensions
{
public static string NullSafeToString(this object obj)
{
return obj != null ? obj.ToString() : String.Empty;
}
}
Then to use:
attribs.something = entry.Properties["something"].Value.NullSafeToString();
Solution 3 - C#
Convert.ToString(entry.Properties["something"].Value);
Solution 4 - C#
Adding an empty string to an object is a common idiom that lets you do null-safe ToString
conversion, like this:
attribs.something = ""+entry.Properties["something"].Value;
When entry.Properties["something"].Value
is null
, this quietly returns an empty string
.
Edit: Starting with C# 6 you can use ?.
operator to avoid null
checking in an even simpler way:
attribs.something = entry.Properties["something"].Value?.ToString();
// ^^
Solution 5 - C#
Can you not do:
attribs.something = entry.Properties["something"].Value as string;
Solution 6 - C#
attribs.something = String.Format("{0}", entry.Properties["something"].Value);
Not sure about performance though...
Solution 7 - C#
In C# 6.0 you can do it in a very elegant way:
attribs.something = entry.Properties["something"].Value?.ToString();
And here is an article about new null-conditional operator.
Solution 8 - C#
As a variation to RexM's answer:
attribs.something = (entry.Properties["something"].Value ?? attribs.something).ToString()
The only downside would be that the attribs.something would be assigned a value (itself, in this example) even if entry.Properties["something"].Value was null - which could be expensive if the .something property did some other processing and/or this line executes a lot (like in a loop).
Solution 9 - C#
To do precisely what you're trying to do a helper method can always be used:
CopyIfNotNull(entry.Properties["something"].Value, out attribs.something);
void CopyIfNotNull(string src, out string dest)
{
if(src != null)
dest = src;
}
Solution 10 - C#
attribs.something = string.Format("{0}",entry.Properties["something"].Value)
Solution 11 - C#
Is it somehow possible to do something like https://stackoverflow.com/questions/550374/checking-for-null-before-tostring/550399#550399">Dale Ragan's answer above, but overriding ToString() instead of creating a new NullSafeToString() method? I'd like this (or returning "null") to be the default behaviour. The compiler (Visual C# 2010 Express) doesn't complain when I add the following method to public static class ObjectExtensions, but the method doesn't get called...
public static String ToString(this Object obj)
{
if (obj == null)
{
return "null";
}
else
{
return obj.GetType().Name;
}
}
Solution 12 - C#
How about using an auxiliary method like this:
attribs.something = getString(
entry.Properties["something"].Value,
attribs.something);
static String getString(
Object obj,
String defaultString)
{
if (obj == null) return defaultString;
return obj.ToString();
}
Alternatively, you could use the ??
operator:
attribs.something =
(entry.Properties["something"].Value ?? attribs.something).ToString();
(note the redundant ToString()
call when the value is null
)