What is the difference between an empty string, null, and IsNullOrWhiteSpace?

C# Strings Basic Fresher Conceptual 1 min read

Quick Answer

null means “no string reference” — calling a method on it throws. "" (or string.Empty) is a real, zero-length string. string.IsNullOrEmpty(s) is true for both. string.IsNullOrWhiteSpace(s) is also true for " " — use it for validating user input.

null is the absence of a string — if you call .Length on it you get a NullReferenceException. An empty string is a genuine string object with zero characters. string.IsNullOrEmpty checks for either of those. And string.IsNullOrWhiteSpace goes further and also returns true for a string that's just spaces or tabs, which is what I actually use when validating that a user typed something meaningful into a field.

  • Prefer string.Empty over "" for clarity (both are the same interned instance).
  • For most user-input validation, IsNullOrWhiteSpace is what you want — a form field with only spaces isn't a real value.
  • Don't confuse an empty string with null in a database context — they map to different things and can break unique constraints or joins.