This code is from book named "Pro C# with .NET 6" and I wonder what happens behind the scenes while the reference to reference(ref Person p) is passed to the method. for example when we do p.personAge = 555; does dereferencing happen twice behind the scenes to first get to the variable which is being pointed to by p and then to get to value which the variable pointed by p contains? sorry if question is bit confusing but overall I want to know how dereferencing works in C#, in this case do we have 2 way dereferencing which happens automatically by the compiler?
static void SendAPersonByReference(ref Person p)
{
// Change some data of "p".
p.personAge = 555;
...
}
does dereferencing happens in this code too?
static void SendAPersonByReference(Person p)
{
// Change some data of "p".
p.personAge = 555;
...
}
Yes, there is a double-dereference; this is encoded in the IL; see here, with the key portion being:
(by-reference)
vs (by value)
The
ldind.refis the extra dereference, from aref Person(a reference to a reference) to aPerson(a reference).