Since this is my first attempt at an extension method, which seems very useful to me, I just want to make sure that I am following the correct route
public static bool EqualsAny(this string s, string[] tokens, StringComparison comparisonType) { foreach (string token in tokens) { if (s.Equals(token, comparisonType)) { return true; } } return false; }
Called
if (queryString["secure"].EqualsAny(new string[] {"true","1"}, StringComparison.InvariantCultureIgnoreCase)) { parameters.Protocol = Protocol.https; }
EDIT: Some great suggestions I tried are exactly what I was looking for. Thanks
EDIT:
I decided the following implementation
public static bool EqualsAny(this string s, StringComparison comparisonType, params string[] tokens) { // for the scenario it is more suitable for the code to continue if (s == null) return false; return tokens.Any(x => s.Equals(x, comparisonType)); } public static bool EqualsAny(this string s, params string[] tokens) { return EqualsAny(s, StringComparison.OrdinalIgnoreCase, tokens); }
I preferred using params over IEnumerable because it simplified the calling code
if (queryString["secure"].EqualsAny("true","1")) { parameters.Protocol = Protocol.https; }
Far above the previous
if (queryString["secure"] != null) { if (queryString["secure"] == "true" || queryString["secure"] == "1") { parameters.Protocal = Protocal.https; } }
Thanks again!
Nick allen
source share