Erwin's Blog

Developing with .NET
Interesting links week #32 and #33

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 #30 and #31

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

Interaction:

Frontend:

Development:

Mobile:

Marketing:

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

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: , , ,
Interesting links week #24 and #25

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

Interaction:

Frontend:

Development:

Mobile:

Marketing:

Other:

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

Interesting links week #22 and #23

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 #3: String.PadLeft

And there is part 3, String.PadLeft, this method will add a specified character to the left side of string till the specified length is met.

The method definition:

public string PadLeft(int totalWidth, char paddingChar)

So what can you to with the PadLeft method in practice? The following code sample will add leading zero’s only when it’s needed:

   1:  using System;
   2:   
   3:  public class MyClass
   4:  {
   5:      public static void Main()
   6:      {
   7:          // Displays: 001
   8:          Console.WriteLine("1".PadLeft(3, '0'));
   9:          
  10:          // Displays: 010
  11:          Console.WriteLine("10".PadLeft(3, '0'));
  12:          
  13:          // Displays: 100
  14:          Console.WriteLine("100".PadLeft(3, '0'));
  15:          
  16:          Console.ReadLine();
  17:      }
  18:  }

As you can see if the string length is already equal to the total width of the PadLeft the zero isn’t added.

There is also an overload of the PadLeft method, this method will add spaces till the total width is met.

public string PadLeft(int totalWidth)

As you probably expected there is also a PadRight method, this will do the opposite of the PadLeft method.

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

Posted: May 31 2011, 08:06 PM by erwin21 | with 2 comment(s)
Filed under: , ,
Interesting links week #21

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 #19 and #20

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

Frontend:

Development:

Mobile:

Other:

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

More Posts Next page »