Coder Perfect

When you call an extension method on a null object in C#, what happens?


Is there a null reference exception thrown when the method is called with a null value?

MyObject myObject = null;
myObject.MyExtensionMethod(); // <-- is this a null reference exception?

If this is the case, will I never need to check for null in my ‘this’ parameter?

Asked by tpower

Solution #1

That will suffice (no exception). Extension methods don’t use virtual calls (i.e. it uses the “call” il instruction, not “callvirt”) so there is no null check unless you write it yourself in the extension method. This is actually useful in a few cases:

public static bool IsNullOrEmpty(this string value)
    return string.IsNullOrEmpty(value);
public static void ThrowIfNull<T>(this T obj, string parameterName)
        where T : class
    if(obj == null) throw new ArgumentNullException(parameterName);


Calls to static calls are, at their core, quite literal – i.e.

string s = ...
if(s.IsNullOrEmpty()) {...}


string s = ...
if(YourExtensionClass.IsNullOrEmpty(s)) {...}

There is no null check in this case.

Answered by Marc Gravell

Solution #2

Marc Gravell’s accurate answer has been supplemented.

If it’s evident that this argument is null, the compiler may provide a warning:


Runtime performance is good, but the warning appears “A System is always the result of expression. Because the default value of string is null, a NullReferenceException is thrown “..

Answered by Stefan Steinegger

Solution #3

Because extension methods are merely glorified static methods, they will be called with null references given in without throwing a NullReferenceException, as you’ve already discovered. However, because they seem to the caller as instance methods, they should behave as such. Most of the time, you should verify this argument and throw an exception if it is null. It’s fine not to do this if the method explicitly handles null values and its name clearly states such, like in the following examples:

public static class StringNullExtensions { 
  public static bool IsNullOrEmpty(this string s) { 
    return string.IsNullOrEmpty(s); 
  public static bool IsNullOrBlank(this string s) { 
    return s == null || s.Trim().Length == 0; 

I also wrote a blog post about it a while back.

Answered by Jordão

Solution #4

The extension method will get a null parameter.

Yes, an exception will be thrown if the method tries to access the object without first testing if it is null.

A guy here wrote “IsNull” and “IsNotNull” extension methods that check is the reference passed null or not. Personally I think this is an aberration and shouldn’t have seen light of day, but it’s perfectly valid c#.

Answered by Binary Worrier

Solution #5

As others have pointed out, invoking an extension method on a null reference results in the this argument being null and nothing else happening. This inspires the concept of writing guard clauses with extension methods.

You can find several examples in this article: How to Make Cyclomatic Complexity Less Complicated: Clause of Protection The short version is as follows:

public static class StringExtensions
    public static void AssertNonEmpty(this string value, string paramName)
        if (string.IsNullOrEmpty(value))
            throw new ArgumentException("Value must be a non-empty string.", paramName);

This is the extension method for the string class that can be called on a null reference:


The call is successful only because the extension function is successfully called on a null reference by the runtime. Then you can use this extension technique to add guard clauses to your code without having to worry about cumbersome syntax:

    public IRegisteredUser RegisterUser(string userName, string referrerName)




Answered by Zoran Horvat

Post is based on