Strings get even better with .NET 4

Handling text has always been the bane of my programming career.  Starting off as a C developer forever tainted me when it comes to processing text; it’s difficult for me to not cringe every time I think of having to do text parsing.  String manipulation in .NET was a pleasant surprise for me – when I started with C# I found I suddenly lost my urge to wince every time I had to deal with text input, and no longer was I itching to jump over to a loosely typed language just for handling string manipulation.  The System.String class in .NET provided most of the ease of use I missed in C (and even C++).

However, even in .NET, there have been little annoyances when dealing with the String class.  Version 4 of the .NET framework, again, adds some functionality to simplify day to day programming tasks.  It’s really nice to see that the base class library team is giving some attention to the core, very common classes.  Here is a summary of the changes to the String class, and why they help.

String.IsNullOrWhiteSpace – Making text validation even easier

It’s common to want to verify that a string isn’t empty and that the string isn’t only whitespace.  In .NET 3.5sp1, this was a rather lengthy check:

bool valid = !string.IsNullOrEmpty(stringToCheck) && (stringToCheck.Trim() != string.Empty);

This one was an easy one to “get wrong” as well, since the Trim() call will throw a NullReferenceException if you pass it a null string, so you have to check for null before you can try trimming the string.  .NET 4 simplifies this to a single method:

bool valid = string.IsNullOrWhiteSpace(stringToCheck);

Much cleaner, easier to follow, and easier to get right from the start.

String.Join – Code gets cleaner with params and objects

The String.Join method gets nicer in .NET 4, as well.  First, the syntax has changed, with the addition of the params keyword.  This means we no longer need to explicitly create arrays to join a series of strings.  A new overload taking an array (with params) of object values was also added.  These changes simplify the usage of strings in many scenarios, letting you do simple things like:

void Log(string message, User user)
{
    Debug.WriteLine(string.Join(" - ", DateTime.Now, user, message));
}

The above code will write out a string to the output window, joining together the arguments, and automatically calling ToString() on the user and DateTime parameters.  Compare to the .NET 3.5 equivelent:

void Log(string message, User user)
{
    Debug.WriteLine(string.Join(" - ", new string[] { DateTime.Now.ToString(), user.ToString(), message}));
}

This is just much nicer to type and read later.

IEnumerable<string> with String.Concat and String.Join – LINQ friendly string methods

Sometimes it’s obvious that the string methods were designed back in .NET 1.1.  String.Concat() and String.Join(), for example, both work on a string[] instead of any of the many options that would have made more sense in .NET 2.0 and later, such as IList<T>, ICollection<T>, or (best of all) IEnumerable<T>.  This has finally been corrected via new overloads.  Now you can pass the results of a LINQ statement or an Enumerable extension method directly into these methods.  For example:

string joinedString = string.Join(", ", users.Select( u => string.Join(" ", u.FirstName, u.LastName) ) );

This shows a nice example of using the new params option, as well as the overload taking IEnumerable<T>, in order to generate a clean list of users by name, separated by commas.

About Reed
Reed Copsey, Jr. - http://www.reedcopsey.com - http://twitter.com/ReedCopsey

Comments

One Response to “Strings get even better with .NET 4”

Trackbacks

Check out what others are saying about this post...
  1. […] This post was mentioned on Twitter by Reed Copsey, Jr., Nick Leuchtenberg. Nick Leuchtenberg said: Nice! http://reedcopsey.com/?p=79 […]

    [WORDPRESS HASHCASH] The comment’s server IP (208.74.66.43) doesn’t match the comment’s URL host IP (74.112.128.10) and so is spam.



Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!