Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#
Tip/Trick

Check for empty string using length property of the string variable

Rate me:
Please Sign up or sign in to vote.
2.00/5 (1 vote)
23 Jun 2010CPOL 10.8K   3
Most of the developers use string.Empty to check the empty string. Instead we can check and compare the length property is zero to know the string value is empty or not.
This will increases the performance :thumbsup:

Example:

string empName = txtEmployeeName.Text;

if(empName.Length <= 0)
Response.Write("Employee name is empty");

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer HCL Technologies Limited
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 2 This is good trick if you don't care... Pin
mazaaz24-Jun-10 1:05
mazaaz24-Jun-10 1:05 
QuestionWhay if a string is null? Pin
Ankur\m/23-Jun-10 3:40
professionalAnkur\m/23-Jun-10 3:40 
I usually use
C#
String.IsNullOrEmpty(txtEmployeeName);

This also checks for null values. And considering the systems that we use today, the performance gap would be negligible.

Correct me if I am wrong.

--
Ankur
..Go Green..

AnswerRe: Whay if a string is null? Pin
Prakash.LE23-Jun-10 20:03
Prakash.LE23-Jun-10 20:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.