Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: .NET
Expertise: Intermediate
Oct 19, 2009



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date