C# 4.0-string.IsNullOrWhiteSpace()

We already have string.IsNullOrEmpty() method to check whether the string is null or empty.Now with Microsoft.NET Framework 4.0 there is a new another method that is called string.IsNullOrWhiteSpace() which will also check whether string is having whitespaces or not with null and empty string.This method is help full where we can check string is having whitespace rather then its empty or null. It will return a true if a string is empty,null or its having whitespaces. Let’s create a simple example and will see how its works. Here we are going test it with 4 options Empty string,Normal String,White spaces and Null String. Following is a code for that.

class Program
static void Main(string[] args)
string TestString = string.Empty;
Console.WriteLine("Test with empty string:{0}",
TestString = "Normal String";
Console.WriteLine("Test with normal string:{0}",
TestString = " ";
Console.WriteLine("Test with whitespace:{0}",
TestString = null;
Console.WriteLine("Test with null:{0}",
Following is a output of the above code here you can see its returning false with Normal string and for all other options its returning true as expected.

Shout it
kick it on DotNetKicks.com


Popular posts from this blog

How to create Rest API(Web API) with ASP.NET Core 1.0

Creating PDF with ASP.Net MVC and RazorPDF

Solution: gulp is not recognized as internal or external command- Visual Studio