1

I have a node class. Variables of this type may be null, the node class contains a pointer variable to another node. If the node class is null, how do I change the getter to return null when attempting to access the pointer to the next node? My code:

        public class Node
        {
            private T _data;
            private Node _next;

            public T Data 
            {
                get => _data;
                set
                {
                    if (this != null)
                    {
                        _data = value;
                    }
                }
            }
            public Node Next 
            {
                get
                {
                    if (this != null)
                    {
                        return _next;
                    }
                    return null;
                }
                set
                {
                    if (this != null)
                    {
                        _next = value;
                    }
                }
            }

            public Node(T data)
            {
                Data = data;
                Next = null;
            }
        }

I tried adding a null check to the getter for the Next property, but it doesn't appear to be working.error message

Ioan
  • 25
  • 4
  • A quick-fix could be to use that null-conditional operator (https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-). A more readable option might be to let the calling code handle the null-check. – Testcase Feb 23 '23 at 18:39

2 Answers2

1

Short answer is - no, if object is null you can't call any properties/methods of it at all and hence no code in the method/property will be able to handle such cases.

If you really want similar syntax you can switch to extension methods that are static and hence can actually check if value is null before the call. Note that it will likely to be very confusing/surprising for readers of the code that calling method on null actually succeed.

public static Node GetNext(this Node n) => n?.Next;

You can also consider if null object coding pattern works for your case.

Notes

  • this != null is generally pointless check for regular code as this can't be null in normal cases (see Within a C# instance method, can 'this' ever be null?)
  • maybe switching to use null-conditional operator ? like node?.Next directly in the code (instead of extension as suggested above) would be enough.
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

You can't access properties of a null object in c#. Because myNode is null when you try to access myNode.Next, it can't be accessed at runtime, simply because it doesn't exist.

An analogy I like to use is doors and what's behind them. If an object is created, but all of it's properties are null, it's like walking into a room that's completely empty. If the object is null, it's like the door has been barred shut. You can't access the room, so when I ask you if there's a couch in that room, you don't know because you can't look inside. The only way to look inside an object is to first instantiate it.

Josh Heaps
  • 296
  • 2
  • 13