Skip to content

Extension Methods on null

Question

Can extension method be called on variable which holds null?

Short interview answer

Yes. Extension-method syntax is compiled as a static method call, so the receiver can be passed as null. The method must explicitly handle that value before dereferencing it.

Detailed answer

Given public static bool IsMissing(this string? value) => string.IsNullOrEmpty(value);, calling text.IsMissing() is valid even when text is null: the static method receives null as its first argument. An extension method that immediately accesses value.Length will still throw NullReferenceException. This differs from an instance method call, which needs a non-null receiver before its body can run. Use this behavior sparingly and make a null-accepting extension method obvious through its name and nullable annotations.

Sources