How to check for a null string in C#.Net

Hi Experts,
I need help checking for null values in my C#.Net program.

Here is a sample of my code, how would I check for null values in order to prevent my program from crashing?

SAMPLE CODE:
PatientLName = reject.PatientLName.ToString();

Open in new window


Thank you very much in advance,
mrotor
mainrotorAsked:
Who is Participating?
 
Dirk StraussConnect With a Mentor Senior Full Stack DeveloperCommented:
Does reject.PatientLName return a string value by default? In other words, is it a string property?
You could check to see if the object reject is null first. Something along the lines of:
if (!(reject == null))
{

}

Open in new window

Then you can use IsNullOrWhiteSpace or IsNullOrEmpty on your string. With IsNullOrEmpty, it checks if a string is null or a blank string. With IsNullOrWhiteSpace, it checks if a string is null or an arbitrary number of spaces in the string (including a blank string).

I just always use IsNullOrWhiteSpace because I want to catch nulls as well as blank spaces.
string myString = null;
if (!(String.IsNullOrWhiteSpace(myString)))
{    
    Console.WriteLine("The string has a value");
}
else
{
    Console.WriteLine("The string value is null");
}

Open in new window

C# Pad reference
Lastly, if you are using C#6 or higher, you can do this on your reject class property:
public string PatientLName { get; set; } = ""; // C# 6 or higher

Open in new window

This way you can default the value to an empty string.
0
 
Pawan KumarDatabase ExpertCommented:
You can use

Convert.ToString(reject.PatientLName)

or

string.ISNULLorEmpty(reject.PatientLName)
1
 
John TsioumprisSoftware & Systems EngineerCommented:
I use the
string.Length>0

Open in new window

to determine if the string is null or empty
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Éric MoreauSenior .Net ConsultantCommented:
John, if the string is null, querying the Length property will trigger an error!
1
 
zephyr_hex (Megan)DeveloperCommented:
I don't like the idea of defaulting a string to empty instead of null.  Null is easy to test for, and an empty string obfuscates whether the property has been set or not.  How can you tell if the value is "" or whether it has not been set if the default is empty string?

It's better to not set a default value.
0
 
sarabandeCommented:
I don't like the idea of defaulting a string to empty instead of null.
in my opinion it makes absolutely no sense to test each string member for being null. this check would be necessary for any access to that member and would blow up your code for no advantage. same as in a real print form string members should be empty if they don't have a value what easily could be checked if necessary (and only there).

How can you tell if the value is "" or whether it has not been set if the default is empty string?
why did you want to know that difference?  if you initialize all variables with a valid value, an empty string variable means that it has no value. that is much better than for other types where you either have to define your own 'no value' or have to test for null, even if you simply want to call ToString() function.

Sara
1
 
käµfm³d 👽Commented:
to determine if the string is null or empty
How does that tell you the string is null? Do you explicitly catch an exception? If so, that seems awfully expensive.

How can you tell if the value is "" or whether it has not been set if the default is empty string?
I could argue the same about using null, since you can assign a variable null after declaration.

Lastly, if you are using C#6 or higher, you can do this on your reject class property:
If you're using C# 6, then why not just use the null conditional operator?

in my opinion it makes absolutely no sense to test each string member for being null.
I'd say that depends on your team's convention.
1
 
zephyr_hex (Megan)DeveloperCommented:
I could argue the same about using null, since you can assign a variable null after declaration.

I think you're missing the point (or maybe I wasn't clear).  If you set a string to null, you are intentionally stating that it has no value.  You then know it has no value.  If the default is empty string, you have no idea if the string has a value that's empty, or whether it has no value at all.
0
 
käµfm³d 👽Commented:
Perhaps you are missing mine. The fact that null means something has no value is effectively a convention. The creators of the language could have easily said that empty string means no value. But the compiler defaults to null. That said, I can still just as easily treat empty string as meaning the value wasn't set. Or I can do the same with null. I'm not saying it's right or that it makes complete sense, but I could do it.
1
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Just 2 add my 2 cents I would argue that String.Empty is a valid value versus null which is to say there is a lack of a defined value.  Of course it's all up to individual conventions such as using -1/1/0 in an int field to represent no value / true / false  versus null / 1 / 0 in a bit field.
0
 
Pawan KumarDatabase ExpertCommented:
@Author -

Have you tried my first comment - Both the options would have worked for you.

Thanks
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
@Pawan, not if reject itself is null.
0
 
Pawan KumarDatabase ExpertCommented:
@kyle - meaning?
0
 
sarabandeCommented:
Kyle means that your statement

String.IsNullorEmpty(reject.PatientLName) 

Open in new window


would throw an exception if reject is null,

it would not throw an exception if reject.PatientName was null.

if you were using IsNullOrEmpty your code makes no difference between null or empty what is a pro for the argument that making a difference is a useless and inefficient complication.

i am a c/c++ programmer where null objects can be defined by using pointers. however, the usage of pointers at places where it is not necessary is looked on as bad programming and this applies especially for pointer members.

Sara
1
 
Pawan KumarDatabase ExpertCommented:
@Kyle - Question is to check for reject.PatientLName not for reject.
0
 
Kyle AbrahamsConnect With a Mentor Senior .Net DeveloperCommented:
The question is
how would I check for null values in order to prevent my program from crashing
 If you don't consider reject itself that's not a complete answer.

Since no one has yet provided a complete answer I would use:
if (reject != null && reject.PatientLName != null)  // use !string.IsNullOrEmpty(reject.PatientLName) if you want to ensure there's a non empty string in field.
{
  // value is not null
}

Open in new window

1
 
Pawan KumarDatabase ExpertCommented:
My comment and Dirk's comment has completely answers the question.
It is better to delete since the author has not responded to any of the comment.
0
 
sarabandeCommented:
All comments have been valid regardless whether you share the opinion or not. Why should a valuable discussion regarding the difference between null and empty deleted. The Author has got enough good and working code for to resolve their problem.

An assist doesn't need to contain a complete solution. Kyle's answer is the only one which provides a full answer to the original post.

Sara
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.