advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Average Rating: 4.7/5 | Rate this item | 3 users have rated this item.
Expertise: Intermediate
Language: .NET
October 19, 2009
Use Array Predicates to Simplify Searches

Array predicates are a not-so-well-known feature introduced with the .NET Framework 2.0 that let you retrieve specific items or perform operations on all items of an array. For example, a quick way to ensure that none of the parameters passed to a function are empty or null is to use the Any predicate.

As an example, assume a method takes four string parameters. Obviously, one way to determine if any of them are empty is to iterate through the parameters testing each one in turn, but a better method is to use the Any operator. Here's an example:

//gather all the input values.
string[] inputValues = {databaseKey, query, databaseType, password};
//check if the arguments are empty.
if (IsEmpty(inputValues))
   throw new ArgumentException("All values are required.");


//Function to check if the parameter array has any empty values.
private static bool IsEmpty(string[] values)
{
   return (values.Any(value => string.IsNullOrEmpty(value.Trim())));
}
Check out other predicate operations described here.
Srinath MS
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs