Welcome to MSDN Blogs Sign in | Join | Help

jaredpar's WebLog

Code, rants and ramblings of a programmer.

Syndication

News

Now Reading

Expert F#

What's a better book to read when learning F#?

Essential WPF

Thus far the best book I've read on WPF. Gets right down to working with WPF and the goals/history.

Purely Functional Data Structures

Reading this book makes me feel like I'm back in college. It will really get your mind going and is best read with a whiteboard handy.

Blog Roll

Eric Lippert
Dustin Campbell
Jon Skeet
Coding Horror
Brian McNamara
Hub FS
Full List

Calling Extension Methods on Null Objects

One of the gotchas for Extension Methods is that it's legal to call them on Null References.  This isn't really surprising when you think about the feature.  Boiled down to a fundamental level, extension methods are just syntactic sugar for calling a static method and automatically passing the first parameter [1].  However it can catch the unwary off guard. 

The two items that are a little bit different between calling an Instance vs Extension method on a null reference is

  1. The exception (if thrown) will be at the method site instead of the call site.  Not really an issue because you can just jump down the stack frame. 
  2. There may not be an exception thrown.  As long as you don't actually use the extension method target, the code will not necessary throw[2].  For instance consider the following code
    <Extension()> _
    Public Function IsNothing(ByVal o As Object) As Boolean
        Return o Is Nothing
    End Function


    Sub Test()
        Dim x As String = Nothing
        Dim b = x.IsNothing()   ' b = True
    End Sub

This is legal and will not throw.  However I don't recomend that you write it. 

[1] Then again you could also claim that instance methods are just syntactic sugar for calling static methods without having to pass this/Me as the first parameter. 

[2] In some ways this is similar to C++.  C++ doesn't do NULL reference checking automatically (it waits for you to access data on a NULL reference and then crashes if you're lucky).  If you call a method on a NULL pointer but don't actually access any member variables, it will likely run fine. 

Published Friday, November 30, 2007 12:19 PM by Jared Parsons

Filed under: , ,

Comments

# Free People Searches &raquo; Calling Extension Methods on Null Objects @ Friday, November 30, 2007 1:50 PM

PingBack from http://www.absolutely-people-search.info/?p=1518

Free People Searches » Calling Extension Methods on Null Objects

New Comments to this post are disabled
Page view tracker