英文:
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对象编码模式适用于你的情况。
注意
this != null
在常规代码中通常是无意义的检查,因为在正常情况下它不能为null
(参见 https://stackoverflow.com/questions/5055068/within-a-c-sharp-instance-method-can-this-ever-be-null)- 也许直接在代码中使用空值条件运算符
?
,如node?.Next
(而不是上面建议的扩展方法)可能已经足够。
英文:
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 benull
in normal cases (see https://stackoverflow.com/questions/5055068/within-a-c-sharp-instance-method-can-this-ever-be-null)- maybe switching to use null-conditional operator
?
likenode?.Next
directly in the code (instead of extension as suggested above) would be enough.
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论