Check for empty text
An empty string has no characters. A space is still a character.
String.IsNullOrEmpty
Simple example
Is a string with no characters empty?
Start''
ResultTrue
using System;
Console.WriteLine(string.IsNullOrEmpty(""));
True
Advertisements
Everyday examples
A library card has no name entered. Should the form ask for one?
Start''
ResultTrue
using System;
Console.WriteLine(string.IsNullOrEmpty(""));
True
A passenger typed a space in the seat field. Is the raw field empty?
Start' '
ResultFalse
using System;
Console.WriteLine(string.IsNullOrEmpty(" "));
False
The classroom screen says Ready. Is the message empty?
Start'Ready'
ResultFalse
using System;
Console.WriteLine(string.IsNullOrEmpty("Ready"));
False
Run the example
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson