I know there are better ways of getting SharePoint field values then indexing the SPListItem for a particular field and using archaic string manipulation to make things dance. But sometimes when you are retrofitting a legacy application it can be intrusive to the holistic application and cause all sorts of issues in other portions.
While doing a code review today at a client this came up. We were discussing when using the SPListItem indexing approach how to best get safe string values. The extension method in use that we were discussing today was:
public static string ToSafeString
return Equals(obj, null) ? string.Empty : obj.ToString();
Now I don’t think that this is too bad, but doesn’t Convert.ToString() already handle null values? I guess I can see the use since it will show on every derived class depending on the implementation of the extension and might look more natural from a language perspective. Furthermore, I find the value type guard clause unnecessary, and could be simply truncated as written as a helper method i.e.:
return value == null ? string.Empty, obj.ToString();