Identification of null objects in C #

I am making a method call that takes the results of four more method calls as parameters - but the methods that make these calls may or may not be null (sorry if this is a hopelessly obscure sentence). Here's the code, if it makes things more clear:

    public void Inform(Room north, Room south, Room east, Room west)
    {
        this.north = north;
        this.south = south;
        this.east = east;
        this.west = west;

        node.Inform(north.GetNode(), south.GetNode(),
                    east.GetNode(), west.GetNode());
    }

Basically, I want to know if there is a quick and easy way to check if an object is null and just pass "null" to the method in addition to conditional expressions - I do not need to explicitly specify null / not null for all 16 possible options.

EDIT: In response to the confusion, I want to clarify this: most of the time, the objects I pass to the method will not be zero. Typically, objects Roomexist for north, south, east, and west, and if they exist Room, the GetNode () method returns the corresponding object. I want to determine if a given exists Roomto avoid unnecessary referenced excpetions when trying to invoke a method call.

+3
source share
3 answers
 public void Inform(Room north, Room south, Room east, Room west)
    {
        this.north = north;
        this.south = south;
        this.east = east;
        this.west = west;

        node.Inform(GetNode(north), GetNode(south),
                    GetNode(east),GetNode(west));
    } 

    private Node GetNode(Room room)
    {
        return room == null ?  null : room.GetNode();
    }
+4
source

Create an extension method

static Node GetNodeOrNull(this Room room)
{
  return room == null ? null : room.GetNode();
}
+9
source

Ignoring the rest of your code (what I need :)) - you can start using the Null pattern. For example, you have a NullRoom class and its GetNode () return something meaningful. In principle, an actual null reference is never allowed.

+3
source

All Articles