Library reference

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
Program.cs
using System;

Console.WriteLine(string.IsNullOrEmpty(""));
Output
True
Advertisements

Everyday examples

A library card has no name entered. Should the form ask for one?

Start''
ResultTrue
Program.cs
using System;

Console.WriteLine(string.IsNullOrEmpty(""));
Output
True

A passenger typed a space in the seat field. Is the raw field empty?

Start' '
ResultFalse
Program.cs
using System;

Console.WriteLine(string.IsNullOrEmpty(" "));
Output
False

The classroom screen says Ready. Is the message empty?

Start'Ready'
ResultFalse
Program.cs
using System;

Console.WriteLine(string.IsNullOrEmpty("Ready"));
Output
False
Run the example
Terminal
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson
Official documentation