Like other reference types,
string references can be null or unassigned. They can also have a third value: empty.
// Unassigned string s; // Null string s2 = null; // Empty string string s3 = ""; // Also empty string string s4 = String.Empty; // This prints true Console.WriteLine(s3 == s4);
All of these signify a lack of text, but they each mean something slightly different:
- unassigned means that the programmer did not give the variable any value
- null means that the programmer intentionally made the variable refer to no object
- an empty string signifies a piece of text with zero characters. This is often used to represent a blank text field. It can be represented by
The Microsoft Programming Guide suggests using
"" instead of
null to avoid
We can check for null OR empty strings using the static
IsNullOrEmpty(). It’s explained in more detail in the documentation.
Console.ReadLine(), ask the user for input and capture it in a variable.
else statement that checks for a null or empty string. If it is null, print out the message:
"You didn't enter anything!"
Otherwise, print out the message:
"Thank you for your submission!"
Run the program using
What happens when you enter no text and hit Enter?