Search
Categories
  ASP.NET
Visual C#
Visual Basic
.NET Framework
ADO.NET
AJAX
LINQ
Java
 
How To Rating
 
5.0 out of 5


Total Votes: 2
         
1 2 3 4 5
 




How to Check whether a String is Null or Empty in .NET

Author: Waqas Anwar - Posted Date: 16-July-2009 - Category: .NET Framework   
Bookmark and Share
If you have a string variable in your program and you want to check whether it is empty or null you have following options:
string a = null;
string b = "";

To check whether string is null you can use = = operator
Console.WriteLine(a == null);
To Check whether string is empty you can either use = = operator by comparing it with String.Empty property or you can use Equals() method of string class as shown below:
Console.WriteLine(b.Equals(""));
Console.WriteLine(b == String.Empty);

To check both null and empty in one statement you can use IsNullOrEmpty method of string class
Console.WriteLine(String.IsNullOrEmpty(a));
Console.WriteLine(String.IsNullOrEmpty(b));

LEAVE YOUR COMMENTS LEAVE YOUR COMMENTS
 Name (required)  
 Email (required
 Website


 
POSTED COMMENTS POSTED COMMENTS

Thanks for sharing this information. This function can be commonly used. Whenever we get some records from database, we can check them whether they are null or empty in just single statement.... Wish u all the best Sir....

Zaheer Mazhar
.NET 61 Batch
Software Engineer
Next Bridge
Posted by Zaheer Mazhar on Wednesday, January 27, 2010

 


 
 
 

Categories

My Portfolio

Website Links


Copyright @ 2009 EzzyLearning.com