| Muhammad Waseem

About Newsletter
Books
30 .NET Tips
Sponsorship
Null Object Pattern in C#
Feb 10, 2024
2 min read

Null Object Pattern in C#

Sponsor this Newsletter

Today, we will explore the details of the fourth design pattern. In our previous blogs, we delved into the concepts of Decorator and Adapter design patterns Facade patterns.

What is Null Object Pattern ?

This pattern says that when we come up with a situation where we are dealing with null value of some object, don’t throw NULL EXCEPTION. Rather return a object representing NULL value which acts as default value.

When we should use it ?

In .NET, you might consider using the Null Object pattern in the following situations:

  • When designing interfaces or abstract classes, you may want to provide default implementations for certain methods. Instead of returning null for methods that return objects, you can return a Null Object instance that provides default behavior.

  • In code where you frequently check for null references before calling methods or accessing properties, using the Null Object pattern can help simplify the code by eliminating these null checks.

  • If your codebase has many conditional statements to handle null references, employing the Null Object pattern can help reduce the complexity of such logic by providing a default behavior that is automatically used when dealing with null objects.

  • Using Null Object implementations can facilitate unit testing by providing concrete objects instead of null references, making it easier to set up test scenarios without worrying about null-related issues.

It’s important to note that while the Null Object pattern can be useful in certain scenarios, it may not be suitable for all situations. Carefully consider the design and requirements of your application before deciding whether to employ this pattern.

Example

This is one of the most simple situation where we can see this pattern in action.

public record Country
{
    public static readonly Country None = new(string.Empty, string.Empty);
    private Country(string name, string numericCode)
    {
        Name = name;
        NumericCode = numericCode;
    }
    public string Name { get; private set; } = string.Empty;
    public string NumericCode { get; private set; } = string.Empty;
}

So when we come up with situation where Country is NULL we can return None instead of throwing Null Exception like this :

public static Country GetCountryByNumericCode(string numericCode)
{
    if (!string.IsNullOrEmpty(numericCode))
    {
        // Find from countries collection and return
    }
    return None;
}

This article was originally published at https://mwaseemzakir.substack.com/ on Feb 10, 2024 .

Whenever you're ready, there are 3 ways I can help you:

  1. Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
  2. Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
  3. Patreon community : Get access to all of my blogs and articles at one place
Previous Next

Subscribe to Newsletter

Join 9,000 Software Engineers

Buy Me a Coffee

Enjoy my articles? Support me by buying a coffee!

Buy Me a Coffee

Muhammad Waseem

Resources
  • Books
  • Courses
Newsletter
  • Articles
  • Sponsorship
Books
  • 30 .NET Tips
  • 100 .NET Tips (Soon)
Author
  • About Us
  • Contact Us
Policy
  • Privacy Policy
  • Terms and Conditions
Interview
  • C# & .NET
  • Web API

Join my .NET newsletter and stay updated!

© 2025 Muhammad Waseem. All rights reserved.