Small set of C# extension methods to check for null/empty/zero across common types.
Use the .NET CLI:
dotnet add package Kebechet.Extensions.IsNullOrEmpty
usingSystem;usingSystem.Collections.Generic;usingIsNullOrEmpty.Extensions;// Stringsstring?s1=null;boola=s1.IsNullOrEmpty();// truestring?s2=" ";boolb=s2.IsNullOrWhiteSpace();// true// CollectionsIEnumerable<int>?numbers=null;boolc=numbers.IsNullOrEmpty();// truevarlist=newList<string>();boold=list.IsNullOrEmpty();// true when empty// GuidGuid?id1=null;boole=id1.IsNullOrEmpty();// trueGuid?id2=Guid.Empty;boolf=id2.IsNullOrEmpty();// true// Numerics (nullable)int?i=0;long?l=null;double?dbl=0.0;decimal?dec=5m;float?fl=0f;boolg=i.IsNullOrZero();// trueboolh=l.IsNullOrZero();// trueboolj=dbl.IsNullOrZero();// trueboolk=dec.IsNullOrZero();// falseboolm=fl.IsNullOrZero();// trueAll methods are annotated with [NotNullWhen(false)], so Roslyn's nullable analysis understands the null check:
string?name=GetName();if(name.IsNullOrEmpty()){return;}// No nullable warning here — Roslyn knows name is not nullConsole.WriteLine(name.Length);string?:IsNullOrEmpty()IsNullOrWhiteSpace()
IEnumerable<T>?:IsNullOrEmpty()
Guid?:IsNullOrEmpty()
- Numerics (
sbyte?,byte?,short?,ushort?,int?,uint?,long?,ulong?,double?,decimal?,float?):IsNullOrZero()
