c# Coalesce function

When you’ve been working heavily with c# for a few days, coalesce jumps into your head for a lot of c# issues. Heres a little helper to provide coalesce in .net

/// <summary>
/// Returns the first non null value, same as SQL's COALESCE()
/// </summary>
/// <param name="p">Args array</param>
/// <returns>First non null value</returns>
public static String Coalesce(params object[] p)
{
	foreach (Object o in p)
	{
		if (o != DBNull.Value && o != null)
		{
			return o.ToString();
		}
	}
	return "";
}

Leave a Reply

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