Skip to content

Instantly share code, notes, and snippets.

@ericnewton76
Created November 15, 2016 16:41
Show Gist options
  • Save ericnewton76/6b7b93b6ddb9ac20bac50522184d6907 to your computer and use it in GitHub Desktop.
Save ericnewton76/6b7b93b6ddb9ac20bac50522184d6907 to your computer and use it in GitHub Desktop.
A routine for genera
public static partial class Utils
{
/// <summary>
/// Checks each supplied value for a non-null value, returning first one supplied.
/// It is recommend to use EmptyFallbacks(Func&lt;string&gt;) overload for any less than trivial value retrievals.
/// </summary>
/// <example>
/// var value = Util.EmptyFallbacks(dictionary["key"], someothervalue, "default value");
/// </example>
/// <param name="values"></param>
/// <returns></returns>
public static string EmptyFallbacks(params string[] values)
{
if(values == null) return null;
foreach(var value in values)
{
if(string.IsNullOrEmpty(value)) return value;
}
return null;
}
/// <summary>
/// Checks each supplied Func&lt;string&gt; for a non-null value, returning first one supplied.
/// </summary>
/// <example>
/// var value = Util.EmptyFallbacks(()=>Request["value"],()=>ConfigurationManager.AppSettings["value"],()=>"DefaultValue");
/// </example>
/// <param name="values"></param>
/// <returns></returns>
public static string EmptyFallbacks(params Func<string>[] values)
{
if(values == null) return null;
foreach(var execfunc in values)
{
string value = execfunc();
if(string.IsNullOrEmpty(value)) return value;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment