Click here to Skip to main content
11,924,800 members (53,629 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked

IfNull Extension Method

, 3 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
An extension method that gives you a replacement if the Type is null.


This tip explains how to implement and use a Generic IfNull Extension Method in your project.


While working on my current C# project after completing an MS Access VBA project, I really missed the Nz function, so I set about creating one. I initially thought about using a static method but didn't like the idea of that as it wouldn't look nice in the code. I then read How does it work in C#? - Part 3 (C# LINQ in detail) by Mohammad A Rahman and it reminded me about Extension Methods so I gave it a go.

If you would like more extension methods there are some very useful ones in the article Extension Methods in .NET that I found to make sure I wasn't duplicating.

Using the code

In your project/solution, create a class and put in the following code:

using System.Text;
namespace MyExtensions
    public static class Extensions
       public static T IfNull<T>(this T source, T valueIfNull)
            if (source is string)
                string temp = source as string;
                if (!string.IsNullOrEmpty(temp))
                    return source;
                return valueIfNull;
            if (source is StringBuilder)
                StringBuilder temp = source as StringBuilder;
                if (temp == null)
                    return valueIfNull;
                if (temp.Length != 0)
                    return source;
                return valueIfNull;
            if (source != null)
                return source;
            return valueIfNull;

With this extension method, you can add custom code to handle specific classes as I have done with the StringBuilder class. This example handles a zero length instance.

To use the extension method in your project, just reference the namespace and project/assembly and the method will be available to use like this:

string salutation = string.Empty;
Console.WriteLine (salutation.IfNull("Sir/Madam"));

Points of Interest

Changing the namespace of the class to System will allow you to use the extension throughout your project but I'm not sure if that is a good practice or not.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer SSE
United Kingdom United Kingdom
Been a professional programmer for 2 years working in C# and VBA working in the energy industry

You may also be interested in...

Comments and Discussions

GeneralWhy? Pin
springy765-Jul-12 2:04
memberspringy765-Jul-12 2:04 
GeneralRe: Why? Pin
TheFoZ5-Jul-12 2:22
memberTheFoZ5-Jul-12 2:22 
GeneralAnother way Pin
gazmendmehmeti4-Jul-12 2:48
membergazmendmehmeti4-Jul-12 2:48 
AnswerRe: Another way Pin
TheFoZ4-Jul-12 3:33
memberTheFoZ4-Jul-12 3:33 
GeneralRe: Another way Pin
springy765-Jul-12 2:07
memberspringy765-Jul-12 2:07 
GeneralRe: Another way Pin
TheFoZ5-Jul-12 2:25
memberTheFoZ5-Jul-12 2:25 
SuggestionSeparate Extensions Pin
Matthew Searles3-Jul-12 21:12
memberMatthew Searles3-Jul-12 21:12 
GeneralRe: Separate Extensions Pin
TheFoZ3-Jul-12 22:57
memberTheFoZ3-Jul-12 22:57 
GeneralRe: Separate Extensions Pin
Paulo Zemek5-Jul-12 5:48
mvpPaulo Zemek5-Jul-12 5:48 
GeneralRe: Separate Extensions Pin
Matthew Searles5-Jul-12 17:45
memberMatthew Searles5-Jul-12 17:45 
GeneralRe: Separate Extensions Pin
TheFoZ5-Jul-12 22:12
memberTheFoZ5-Jul-12 22:12 
GeneralRe: Separate Extensions Pin
TheFoZ5-Jul-12 22:06
memberTheFoZ5-Jul-12 22:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151125.3 | Last Updated 3 Jul 2012
Article Copyright 2012 by TheFoZ
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid