Empty Object To String (C#)

Typically, using object.ToString() method will cause error if the object is null or empty. Here is the quick workaround:

public static string ToSafeString(this object obj) 
{     
	return (obj ?? string.Empty).ToString(); 
} 

I posted the similar one for VB before here.

Leave a Reply

Your email address will not be published. Required fields are marked *