Erwin's Blog

Developing with .NET

July 2011 - Posts

Interesting links week #28 and #29

Below a list of interesting links that I found this week:

Frontend:

Development:

Mobile:

Marketing:

Interested in more interesting links follow me at twitter http://twitter.com/erwingriekspoor

Interesting links week #26 and #27

Below a list of interesting links that I found this week:

Frontend:

Development:

Mobile:

Marketing:

Interested in more interesting links follow me at twitter http://twitter.com/erwingriekspoor

Nifty .NET Part #4: Enumerable.Any

In part 4 of the Nifty .Net series we have the Enumerable.Any method. The Any method is part of the .NET 3.5 LINQ framework, so it’s only available in .NET 3.5 and upwards. The Any method determines whether a sequence contains any elements.

The method definition:

public static bool Any<TSource>(this IEnumerable<TSource> source)

As you can see the the Any method is an extension method on IEnumerable, so you can use the Any method on al classes that implements IEnumerable, like List<T>, string[].

For example:

   1:  using System;
   2:  using System.Linq;
   3:   
   4:  public class MyClass
   5:  {
   6:      public static void Main()
   7:      {
   8:          string[] foo = { "a", "b", "c" };
   9:          
  10:          if(foo.Any())
  11:          {
  12:              Console.WriteLine("Contains any elements");    
  13:          }
  14:          
  15:          Console.ReadLine();
  16:      }
  17:  }

In this code snippet we determines if the foo array has any elements. To use the Any method we have to add the System.Linq namespace as “using” to our class.

Some of the great advantages of the Any method is that it will stop the enumeration of source as soon as the result can be determined.

Beware don’t use the method .Count() like the following code snippet:

   1:  using System;
   2:  using System.Linq;
   3:   
   4:  public class MyClass
   5:  {
   6:      public static void Main()
   7:      {
   8:          string[] foo = { "a", "b", "c" };
   9:      
  10:             if(foo.Count() > 0)
  11:          {
  12:              Console.WriteLine("Contains any elements");        
  13:          }
  14:          
  15:          Console.ReadLine();
  16:      }
  17:  }
 

The above code snippet uses a lot more resources to determine if a sequence contains any elements, because it have to enumerate through all elements of the collection to get the count. The Any method, as I said earlier, stops as soon as he found an element in the collection.

The Any method has also an overload:

public static bool Any<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)

With this overload you can check if there are any elements in collection that matches the predicate. A code example will look like this:

   1:  using System;
   2:  using System.Linq;
   3:   
   4:  public class MyClass
   5:  {
   6:      public static void Main()
   7:      {
   8:          string[] foo = { "a", "b", "c" };
   9:          
  10:          if(foo.Any(s => s == "a"))
  11:          {
  12:              Console.WriteLine("Contains any elements");    
  13:          }
  14:          
  15:          Console.ReadLine();
  16:      }
  17:  }

This code example will look for any elements that matches with “a”, and again it will stops if as soon as the result can be determined.

In some cases you can beter use “.length > 0”, this property has a better performance compared to the Any method:

   1:  using System;
   2:   
   3:  public class MyClass
   4:  {
   5:      public static void Main()
   6:      {
   7:          string[] foo = { "a", "b", "c" };
   8:      
   9:             if(foo.Length > 0)
  10:          {
  11:              Console.WriteLine("Contains any elements");        
  12:          }
  13:          
  14:          Console.ReadLine();
  15:      }
  16:  }

The snippet above is faster because the length property is updated when an item is added or deleted from the list, so it doesn’t have to enumerate through the list.

You can find more info and examples on the MSDN pages:

Posted: Jul 04 2011, 10:28 AM by erwin21 | with 4 comment(s)
Filed under: , , ,
More Posts