Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 307167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:32:01+00:00 2026-05-12T07:32:01+00:00

Martin Fowler’s Refactoring discusses creating Null Objects to avoid lots of if (myObject ==

  • 0

Martin Fowler’s Refactoring discusses creating Null Objects to avoid lots of

if (myObject == null)

tests.
What is the right way to do this? My attempt violates the “virtual member call in constructor” rule.
Here’s my attempt at it:

public class Animal
{
    public virtual string Name { get; set; }
    public virtual string Species { get; set; }
    public virtual bool IsNull 
    { 
        get { return false; }
    }
}

public sealed class NullAnimal : Animal
{
    public override string Name
    {
        get{ return "NULL"; }
        set { }
    }
    public override string Species
    {
        get { return "NULL"; }
        set { }
    }
    public virtual bool IsNull
    {
        get { return true; }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:32:02+00:00Added an answer on May 12, 2026 at 7:32 am

    I tend to agree with Wyatt Barnett’s answer in that you should show restraint when creating these kinds of “null” objects. That said, there are some nice reasons for doing so. On occasion.

    I also tend to agree with Supertux’s answer in that the whole point of a null object is to not need to check whether or not it is null, so you should lose the IsNull property. If you really feel you need the IsNull property, then read Wyatt’s response again and reconsider.

    And thank you CraigTP for the nice links for more info. Good stuff.

    Now I will assume that in your real code you actually have a constructor that is attempting to set the values of Name or Species (whatever your real code equivalent might be called). Otherwise, why would you get the “virtual member call in constructor” warning/error? I’ve run into a couple of similar problems when using the newfangled MyProperty { get; set; } shortcut myself (particularly when used in structs, and don’t get me started about serialization versioning). Your solution is to not use the shortcut, but instead do it the old-fashioned way.

    public class Animal {
        protected Animal() { }
    
        public Animal(string name, string species) {
            _Name = name;
            _Species = species;
        }
    
        public virtual string Name {
            get { return _Name; }
            set { _Name = value; }
        }
        private string _Name;
    
        public virtual string Species {
            get { return _Species; }
            set { _Species = value; }
        }
        private string _Species;
    }
    
    public sealed class NullAnimal : Animal {
        public override string Name {
            get { return String.Empty; }
            set { }
        }
        public override string Species {
            get { return String.Empty; }
            set { }
        }
    }
    

    This solves the problem of setting your virtual properties in the constructor. Instead, you are setting your private field values (something you don’t have the ability to reference if you use the shortcut). For extra credit, compile both methods, and use the Reflector to look at the resulting assemblies.

    The more I use the { get; set; } shortcut, the more I dislike it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Patterns of Enterprise Application Architecture, Martin Fowler writes: This book is thus about
According Martin Fowler's Unit of Work description: Maintains a list of objects that are
I am new to git but read this article by Martin Fowler where he
I am a fan of Martin Fowler's (deprecated) model-view-presenter pattern. I am writing a
I don't understand what's the difference between those patterns Martin Fowler says : Transaction
Currently I'm trying to implement Transaction Script pattern (Exactly how Martin Fowler described by
I am following the presentation model design pattern suggested by Martin Fowler for my
I'm reading Martin Fowler's Book about enterprise application architecture design patterns, but the German
I was reading about DSLs (Martin Fowler's book) and in the first chapter he
I was reading an article put together by Martin Fowler regarding Composed Regular Expressions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.