Erwin's Blog

Developing with .NET

May 2011 - Posts

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

Nifty .NET Part #2: Enumerable.Empty<T>

In part 2 the generic method Enumerable.Empty<T>, as the name would say it returns a empty IEnumerable of T:

public static IEnumerable<TResult> Empty<TResult>()

Lets see how it looks in code, for example we create a method that can return a list of strings:

   1:  using System;
   2:  using System.Linq;
   3:  using System.Collections.Generic;
   4:   
   5:  public class MyClass
   6:  {
   7:      public static void Main()
   8:      {
   9:          IEnumerable<string> names = GetNames(true);
  10:              
  11:          foreach(string name in names)
  12:          {
  13:              Console.WriteLine(name);    
  14:          }
  15:              
  16:          Console.ReadLine();
  17:      }
  18:      
  19:      public static IEnumerable<string> GetNames(bool condition)
  20:      {        
  21:          if(condition)
  22:          {
  23:              return new string[] { "Tom", "Robin", "Paul", "Dennis" };
  24:          }
  25:          
  26:          return Enumerable.Empty<string>();
  27:      }
  28:  }

So if condition is true the GetNames method will return an array of names, if not return an empty sequence of strings.

So why not return a string array of size 0 (new string[0]), well if we use Enumerable.Empty is more clearly what we want to do and every empty sequence of T will be cached so there is also some performance improvements.

The MSDN page of Enumerable.Empty<T>:

Posted: May 10 2011, 10:30 PM by erwin21 | with 6 comment(s)
Filed under: , , ,
Interesting links week #18

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

Interaction:

Frontend:

Development:

Mobile:

Other:

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

Nifty .NET Part #1: String.Join

In the first part of the nifty .NET series I will blog about the method String.Join. First let see how the method definition looks like:

public static string Join(string separator, params string[] value)

Like the name suggests this method can join a bunch of strings together with a given separator.

Below an example of the method:

   1:  using System;
   2:   
   3:  public class MyClass
   4:  {    
   5:      public static void Main()
   6:      {
   7:          string[] array = new String[] { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog" };
   8:          
   9:          string phrase = String.Join(" ", array);
  10:          
  11:          Console.WriteLine(phrase);
  12:          
  13:          Console.ReadLine();
  14:      }
  15:  }

You probably already guessed it, that the output of this console application will be “The quick brown fox jumps over the lazy dog”  because I used a space as separator.

With the overload below you can Join a specific part of the array together:

public static string Join(string separator, string[] value, int startIndex, int count)

There are also some more overload and generic variations on the String.Join listed below:

And for the completeness the msdn pages of the methods discussed above:

From now on you will find some cases where its very nifty to use the String.Join method.

Posted: May 09 2011, 10:48 PM by erwin21 | with 5 comment(s)
Filed under: , ,
Nifty .NET Introduction

This is an start of a new blog series called: “Nifty .NET”. In this series I will blog about all the nifty .NET things, like classes, methods, configurations, etc.

Maybe you already know, maybe not, but I want to remind you about the nifty things .NET has, so at the end we all write more beautiful code than before.

I will update this blog post with all the parts of this blog series so we get one useful overview:

Posted: May 09 2011, 10:42 PM by erwin21
Filed under: , , ,
Interesting links week #17

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

Frontend:

Development:

Marketing:

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

More Posts