Created
November 15, 2016 16:41
-
-
Save ericnewton76/6b7b93b6ddb9ac20bac50522184d6907 to your computer and use it in GitHub Desktop.
A routine for genera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<string>) 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<string> 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