I have a TextBox. And I want to check if it's empty.
Which way is better
if(TextBox.Text.Length == 0)
or
if(TextBox.Text == '')
?
I have a TextBox. And I want to check if it's empty.
Which way is better
if(TextBox.Text.Length == 0)
or
if(TextBox.Text == '')
?
Farhan answer is the best and I would like to add that if you need to fullfil both conditions adding the OR operator works, like this:
Note that there is a difference between using
string
andString
I think
or
are your best options.
You should use
String.IsNullOrEmpty()
to make sure it is neither empty nor null (somehow):More examples here.
For practical purposes you might also consider using
String.IsNullOrWhitespace()
since a TextBox expecting whitespace as input probably negates any purpose, except in case of, say, letting the user pick a custom separator for stuff.