My first extension method, can it be written better?

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!

+6
c # extension-methods
source share
6 answers
 public static bool EqualsAny( this string s, StringComparison comparisonType, params string[] tokens) { foreach (string token in tokens) { if (s.Equals(token, comparisonType)) { return true; } } return false; } 

With parameters, you don’t need to force rows into the array first.

 var match = "loool".EqualsAny(StringComparison.Ordinal, "hurf", "Durf"); 

Linq-ified (JC + me) with NRE (standard standard):

 public static bool EqualsAny( this string s, StringComparison comparisonType, params string[] tokens) { if(s == null) throw new NullReferenceException("s"); return tokens.Any(x=> s.Equals(x, comparisonType)); } 
+5
source share

Yes! First, you need to check s for null. Also, let it accept any IEnumerable<string> for tokens, not just an array, and then use other linq statements to check:

 public static bool EqualsAny(this string s, IEnumerable<string> tokens, StringComparison comparisonType) { if (s== null) return false; return tokens.Any(t => s.Equals(t, comparisonType)); } 

Thinking about how to handle the null value for s, there is a third option that no one has yet used:

  public static bool EqualsAny(this string s, IEnumerable<string> tokens, StringComparison comparisonType) { if (s== null) return tokens.Any(t => t == null); return tokens.Any(t => s.Equals(t, comparisonType)); } 

Finally, regarding the implementation of your choice: if you have overloads, you may also have IEnumerable overloads, and your params code calls them.

+7
source share

Another variant. This will simplify your call site, because if you have several lines that match you, you won’t have to create an array or a list in the code.

  public static bool EqualsAny(this string s,StringComparison comparisonType, param string[] tokens ) { return EqualsAny(s,comparisonType,tokens); } public static bool EqualsAny(this string s,StringComparison comparisonType, IEnumerable<string>tokens ) { //Throw nullReference to keep the semantics aligned with calling an instance member if (s==null) throw new NullReferenceException(); foreach (string token in tokens) { if (s.Equals(token, comparisonType)) { return true; } } return false; } 
+3
source share

Make your tokens parameter more general - i.e. make it IEnumerable<string> .

In addition, an equivalent method already exists that extends IEnumerable<> , for example. Any :

  public static bool EqualsAny(this string s, IEnumerable<string> tokens, StringComparison comparisonType) { return tokens.Any(t => s.Equals(t, comparisonType)); } 

In addition, Joel, of course, is right: you can check for null values ​​before performing actions (security coding). This is not more secure, but facilitates the localization of errors.

+2
source share

to make EqualsAny easier to use, you can use varargs and the default strategy for StringComparison :

 public static bool EqualsAny(this string s, params string[] tokens) { return EqualsAny(s, StringComparison.InvariantCultureIgnoreCase, tokens); } public static bool EqualsAny(this string s, StringComparison stringComparison, params string[] tokens) { // your method } 

Called

 if (queryString["secure"].EqualsAny("true", "1")) { parameters.Protocol = Protocol.https; } 
+1
source share

There is nothing wrong with what you do. However, this type of functionality already exists in several different models.

Example:

 var candidates = List<SomeObject>(); if (candidates.Count(c=> string.Compare(c.PropertyValue, queryString["secure"], StringComparison.InvariantCultureIgnoreCase) == 0) > 0) { parameters.Protocol = Protocol.https; } 
0
source share

All Articles