术语 "dereferencing" 一个对象到底是什么意思?
What exactly does mean the term "dereferencing" an object?
我正在阅读 C# 8 中称为可空引用类型的新功能的描述。该描述讨论了所谓的 null-forgiving 运算符。描述中的示例讨论了引用类型实例的取消引用(我认为是):
You can also use the null-forgiving operator when you definitely know
that an expression cannot be null but the compiler doesn't manage to
recognize that. In the following example, if the IsValid
method
returns true, its argument is not null and you can safely dereference
it:
public static void Main()
{
Person? p = Find("John");
if (IsValid(p))
{
Console.WriteLine($"Found {p!.Name}");
}
}
public static bool IsValid(Person? person)
{
return person != null && !string.IsNullOrEmpty(person.Name);
}
Without the null-forgiving operator, the compiler generates the
following warning for the p.Name
code: Warning CS8602
: Dereference of
a possibly null reference.
我的印象是,在 C# 中取消引用对象意味着将其设置为 null。但看起来 Microsoft 将访问对象的 属性 称为取消引用对象。
问题是:当我们谈论引用类型实例而不是托管和非托管指针时,C# 中的取消引用术语是什么意思。
取消引用意味着跟随引用访问实际的底层对象。如果引用是null
,这就出大问题了。
我正在阅读 C# 8 中称为可空引用类型的新功能的描述。该描述讨论了所谓的 null-forgiving 运算符。描述中的示例讨论了引用类型实例的取消引用(我认为是):
You can also use the null-forgiving operator when you definitely know that an expression cannot be null but the compiler doesn't manage to recognize that. In the following example, if the
IsValid
method returns true, its argument is not null and you can safely dereference it:public static void Main() { Person? p = Find("John"); if (IsValid(p)) { Console.WriteLine($"Found {p!.Name}"); } } public static bool IsValid(Person? person) { return person != null && !string.IsNullOrEmpty(person.Name); }
Without the null-forgiving operator, the compiler generates the following warning for the
p.Name
code: WarningCS8602
: Dereference of a possibly null reference.
我的印象是,在 C# 中取消引用对象意味着将其设置为 null。但看起来 Microsoft 将访问对象的 属性 称为取消引用对象。
问题是:当我们谈论引用类型实例而不是托管和非托管指针时,C# 中的取消引用术语是什么意思。
取消引用意味着跟随引用访问实际的底层对象。如果引用是null
,这就出大问题了。