Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does mean the term "dereferencing" an object?

Tags:

c#

dereference

I'm reading the description of the new feature in C# 8 called nullable reference types. The description discusses so called null-forgiving operator. The example in the description talks about de-referencing of an instance of a reference type (I think so):

Microsoft Docs

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.

I was under impression that in C# dereferencing an object means setting it to null. But it looks like Microsoft calls accessing an object's property as dereferencing the object.

The question is: what does mean the dereferencing term in C# when we are talking about a reference type instances, not about pointers managed and unmanaged.

like image 762
usr2020 Avatar asked Sep 24 '20 15:09

usr2020


People also ask

What is dereferencing explain with example?

Dereferencing a variable means accessing the variable stored at a memory address: int i = 5; int * p; p = &i; *p = 7; //*p returns the variable stored at the memory address stored in p, which is i. //i is now 7.

Why is it called dereferencing?

Dereferencing means taking away the reference and giving you what it was actually referring to. A pointer to something really means that your pointer variable holds a memory address of something . But the pointer can also be thought of as a reference to something instead.

What is the meaning of dereference in computer?

To go to an address before performing the operation. For example, in C programming, a dereferenced variable is a pointer to the variable, not the variable itself.

What is dereferencing a pointer means?

Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator.


1 Answers

Dereferencing means following the reference to access the actual underlying object. If the reference is null, this causes a big problem.

like image 163
Joel Coehoorn Avatar answered Oct 20 '22 11:10

Joel Coehoorn