C#处理空类的属性

huangapple go评论72阅读模式
英文:

C# properties to handle a null class

问题

我尝试在Next属性的getter中添加了一个空值检查,但似乎没有起作用。错误信息

英文:

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

答案1

得分: 1

简短回答是 - 如果对象为 null,则根本无法调用其任何属性/方法,因此在方法/属性中的任何代码都无法处理这种情况。

如果你真的想要类似的语法,你可以切换到扩展方法,它们是静态的,因此在调用之前实际上可以检查值是否为 null。请注意,对于代码的读者来说,在 null 上调用方法实际上可能会非常令人困惑/意外成功。

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

你还可以考虑是否null对象编码模式适用于你的情况。

注意

英文:

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

答案2

得分: 0

你无法在C#中访问空对象的属性。因为当你尝试访问myNode.Next时,myNode是null,它在运行时无法访问,简单地因为它不存在。

我喜欢使用的类比是门和门后的内容。如果一个对象已创建,但它的所有属性都是null,就像走进一个完全空荡的房间。如果对象为null,就像门被紧闭起来。你无法访问房间,所以当我问你那个房间里是否有一张沙发时,你不知道,因为你无法看到里面。查看对象内部的唯一方法是首先实例化它。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月24日 02:30:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定