String.IsNullOrWhiteSpace

I recently started working with Visual Studio 2010 and .NET 4.0 and I came across one of the new language features, String.IsNullWithWhiteSpace. Basically, it's an enhanced version of the commonly used String.IsNullOrEmpty. This new enhanced version goes a step forward and checks for whitespaces such as tabs.

String myString = “          “;

 

//returns false

 

Bool result = String.IsNullOrEmpty(myString);

 

//returns true

 

Bool result = String.IsNullOrWhiteSpace(myString);

A simple but convenient feature.

Self Improvement

What differentiates a great developer from an average one? I've recently started a self-improvement campaign to further expand my skills as a developer and as my first task, before diving into anything new, I'm going back and making sure that I truly understand some of the basics, even though I use these things on a daily basis, I wanted to dive deeper and truly understand the "why's". One of my goals is to incorporate more organization into my solutions. One way to do this is by implementing design patterns, we've all read about design patterns at some point but my ultimate goal is not just knowing what these patterns are but knowing when to apply them and incorporate them into real-life scenarios. It takes more than just simply knowing the language to become a great developer.

The Singleton Approach

The singleton creation-pattern restricts an object of a class to a single instance. The class should expose a static instance that provides access to its public members and properties while guaranteeing thread-safety in a multi-threaded environment.  This pattern is typically implemented in scenarios where we have to cache static data and want to store it in a single location.

So how do we implement this pattern and which scenarios become good candidates?

Say we need to create a class that will load an application configuration file into memory and provide methods to retrieve the data. There might be multiple processes and assemblies that will be calling the class, therefore, it might be costly if we have to load the configuration file into memory every time we create an instance of the class. This is a perfect candidate for the singleton pattern. Since the configuration file will contain static data there is no need load it after the initial load.

Let's take a look at the following example. Suppose we have a class "Configuration".

public class Configuration
{
   public static readonly Configuration Instance = new Configuration();

   private Configuration() { }                      

   public string GetData()
   {
      return "configuration data";
   }
}

As you can see, we are exposing a static instance of the class. Out of the box, the static keyword takes care of two of our requirements. It ensures that the instance is created a single time and  the framework guarantees thread safety on static type initialization.  The readonly modifier restricts any calling code from modifying the instance once it has been initialized. In addition, we are implementing a private constructor to avoid accidental instantiation via the "new" operator.

The methods can then be accessed by calling the exposed public instance.

string configurationData = Configuration.Instance.GetData();