C# Null object design pattern

T.Zacks 3,996 Reputation points
2022-07-24T17:38:44.847+00:00

I am looking for a small sample code for Null object design pattern in c#.

I found a sample code from here https://www.tutorialspoint.com/design_pattern/null_object_pattern.htm
which looks fine where factory is implemented to do but that code is in java. so how could I have the same code in c#?

looking for few sample code to implement Null object design pattern using different approach. thanks

Developer technologies C#
0 comments No comments
{count} vote

Accepted answer
  1. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2022-07-25T01:45:45.503+00:00

    Steps are the same in C#, it's just about different syntax and language features. So considering the following steps:

    1. Create the abstract class or interface or your object
    2. Create concrete implementation
    3. Create the null implementation by overriding the methods and returning proper values for the null object

    You can see an example following above steps to implement Null Object pattern in C#, but before jumping to the example, you may want to learn more about the following C# language features which may help you avoid null exceptions:

    Example C# Null Object Pattern

    Here is the interface for animal:

    public interface IAnimal  
    {  
        string GetName();  
        string MakeSound();  
    }  
    

    And two concrete implementations, for duck and cat:

    public class Duck : IAnimal  
    {  
        public string GetName()  
        {  
            return "Duck";  
        }  
        public string MakeSound()  
        {  
            return "Quack quack!";  
        }  
    }  
    public class Cat : IAnimal  
    {  
        public string GetName()  
        {  
            return "Cat";  
        }  
        public string MakeSound()  
        {  
            return "Meow meow!";  
        }  
    }  
    

    Now I'll implement the null object as a singleton:

    public sealed class NullAnimal : IAnimal  
    {  
        private static readonly IAnimal nullAnimal = new NullAnimal();  
        private NullAnimal() { }  
        public string MakeSound()  
        {  
            return string.Empty;  
        }  
        public string GetName()  
        {  
            return String.Empty;  
        }  
        public static IAnimal Instance  
        {  
            get  
            {  
                return nullAnimal;  
            }  
        }  
    }  
    

    Then I'll create a factory to return the animal, based on the name:

    public class AnimalFactory  
    {  
        public static IAnimal GetAnimal(string name)  
        {  
            if (name == "duck")  
                return new Duck();  
            else if (name == "cat")  
                return new Cat();  
            else  
                return NullAnimal.Instance;  
        }  
    }  
    

    And the usage:

    var animals = new[] { "duck", "cat", "unknown" };  
    foreach (var name in animals)  
    {  
        var animal = AnimalFactory.GetAnimal(name);  
        Console.WriteLine($"{animal.GetName()} says: {animal.MakeSound()}");  
    }  
    

    Which returns:

    Duck says: Quack quack!  
    Cat says: Meow meow!  
     says:   
    

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-07-24T18:22:13.377+00:00

    See the following where the null-coalescing operator ?? in the closing example shows a Null Object Pattern.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.