© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
3 replies
Johnnnnnnn

❔ How to replace string.IsNullOrWhiteSpace() with pattern matching?

Hi, as I understand it, MS is now suggesting that we use pattern matching as much as possible for testing values.
So I try to use "is null" everywhere instead of "== null", as has been the norm up to C# 8.
Does anyone here have a suggestion for how to use pattern matching to test if a string is null or whitespace?
What I've come up with so far is
if ("a string".Trim() is { Length: > 0 }) { ... }
if ("a string".Trim() is { Length: > 0 }) { ... }
...
The
Trim()
Trim()
doesn't look so good in my eyes...
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Replace regex-pattern with dynamically generated string.
C#CC# / help
3y ago
✅ Switch pattern matching with integers
C#CC# / help
3y ago
✅ Using Params with Pattern Matching Switch statement
C#CC# / help
2y ago
Replace a part from String
C#CC# / help
4y ago