Maybe some of you are already using this. But for others, it could be a great utility method.
We use a static
method IsNullOrEmpty
of string
in most of our daily task/projects a lot. It works fine in most of the cases, but it could work bizarre in some cases if not handled properly.
Say, you get a entry from UI, and having a check whether user has entered something or not. It would work fine as long as user enters the data or not.
But what happen if user enters just spaces. This method would return false
, ironically this method is behaving as coded. But do we need this?
Obviously not, spaces could lead to wrong entry in our database even can corrupt the data and leads to uncommon results/Y SODS/malfunctioning.
Although being a good developer, one always trims the input before having the check. But it also tends a lots of extra LOCs which we can save and can make our system less error prone.
Most of us are aware of the beauty of the extension methods that were introduced in C# 3.0. So we can have an extension method over a string
, which actually does both; first trimming and then checking for IsNullOrEmpty
.
So we can have the extension method as:
public static bool IsNullorEmpty(this String val)
{
if (val != null)
{
if (string.IsNullOrEmpty(val.Trim()))
return true;
else
return false;
}
return true;
}
One more smarter code would be:
public static bool IsNullorEmpty(this String val)
{
if (val != null)
{
return string.IsNullOrEmpty(val.Trim());
}
return true;
}
Let's see both the existing and our new method running.
My code is:
static void Main(string[] args)
{
string name = " ";
Console.WriteLine(String.IsNullOrEmpty(name));
Console.WriteLine(name.IsNullorEmpty());
Console.ReadKey();
}
The output is:
But if you are still using .NET 2.0, you can have a normal static
method in your utility call, which does the job for us.
public static bool CheckNullorEmpty(string val)
{
if (val != null)
{
if (string.IsNullOrEmpty(val.Trim()))
return true;
else
return false;
}
return true;
}
Note: I must say, the limitation of the existing IsNullorEmpty
has been resolved in .NET 4.0. It means you don’t need to do all this. There is a new method String.IsNullOrWhiteSpace
will do both for you. But unlikely, most of us are still working on .NET2.0/3.5.
Hope you all must have liked it.
Brij is a 3-times Microsoft MVP in ASP.NET/IIS Category and a passionate .NET developer. More than 6 years of experience in IT field, currently serving a MNC as a Tech Lead/Architect.
He is a very passionate .NET developer and have expertise over Web technologies like ASP.NET 2.0/3.5/4.0, jQuery, JSON, Javascript, IIS and related technologies. He is also a Exchange Server (EWS) Specialist. He has great experience in design patterns and N-Tier Architecture.
He is also certified as Microsoft Certified Technologies Specialist-ASP.NET and Microsoft Certified Technologies Specialist-WCF in .NET 4.0. He has also received several awards at various forums and his various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website www.asp.net.
He has done MCA from NIT Durgapur and completed his graduation from Lucknow University.
Learning new technologies and sharing knowledge excites him most. Blogging, solving problems at various forums, helping people, keeps him busy entire day.
Visit his Blog:
Code Wala
Area of Expertise :
C#, ASP.NET 2.0,3.5,4.0, AJAX, JQuery, JSON, XML, XSLT, ADO.Net, WCF, Active Directory, Exchange Server 2007 (EWS), Java script, Web Services ,Win services, DotnetNuke, WSS 3.0,Sharepoint Designer, SQL Server 2000/2005/2008