Because this is my first attempt at an extension method that seems quite useful to me, I just want to make sure I'm going down the right 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 by
if (queryString["secure"].EqualsAny(new string[] {"true","1"}, StringComparison.InvariantCultureIgnoreCase))
{
parameters.Protocol = Protocol.https;
}
EDIT: Some excellent suggestions coming through, exactly the sort of thing I was looking for. Thanks
EDIT:
I have decided on 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;
}
A far cry on the previous
if (queryString["secure"] != null)
{
if (queryString["secure"] == "true" || queryString["secure"] == "1")
{
parameters.Protocal = Protocal.https;
}
}
Thank you again!