code readability - C#: Extension methods and the Not operator Best Practice -


i have array of strings , wish find out if array not contain string. can use not operator (!) in conjunction contains method so:

if (!stringarray.contains(searchstring)) {     //do } 

the not operator (!) might overlooked when scanning code wondering if considered bad practice create extension method in attempt enhance readability:

public static bool doesnotcontain<t>(this ienumerable<t> source, t value) {     return !source.contains<t>(value); } 

so code read:

if (stringarray.doesnotcontain(searchstring)) {     //do } 

is sort of thing frowned upon?

keep !. comment above line readability.
(i suspect ! more efficient)

//if word not in array then... 

another point whether dead-set on using array? there (that may or may not know about) called hashset.

if sole purpose examine whether or not string in list, looking @ set arithmetic.

unless using array other finding out whether term in or not, try using hashset...much faster.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -