✅ How the "out" keyword works internally
Hello guys, sorry to disturb you all, I was just reading a bit about the
out
keyword. From what I have understood, the out
keyword stores the "reference" of a variable tied to it. Any change method to that variable will reflect back when used later on.
I was wondering if someone can explain how the "out" keyword works internally related to what do we mean by "references", how are they stored in memory etc pls.
I have an overview of what happen, but I would really appreciate if someone can explain what's happening under the hood with the internal memory structure. (I think it's the same thing when we use the ref keyword, we modify addresses directly internally)23 Replies
Do you know what a (C-style) pointer is?
yeah, I know a bit
I need to refresh but I know a bit how it works
A reference is the name that C# gives to a pointer (close enough: a reference is a pointer which the GC knows about)
So when you use
ref
or out
, instead of passing a value directly, the compiler passes a pointer, and the pointer contains the memory address that the value is stored atyeah I see, but compared to C language where we would need to dereference the pointer etc, here we work directly with the variable
how is that possible
like it seems behind the scenes, everything is being done
The compiler knows that it's a pointer, and does the derefencing for you
ah
Other languages do the same. See e.g. C++ and
&
references, or rust and auto-derefyep I see
so for e.g, if I have something like
out int result
, what's happening is we are storing a reference to result, this allows us to modify the content of result directly and not a local copy of it. Now, when we write something like result = 20
, behind the scenes, the compiler knows when to dereference things etc ? (was wondering, in which case would we need to dereference things here, like I was thinking when we write result = 20
; behind the scenes, dereferencing occurs, but not sure)(In C, you need to disambiguate between
*pFoo = newValue
and pFoo = newValue
(assigning a new value to the memory pointed to by the pointer, vs assigning a new memory address to the pointer itself). C# uses a different syntax for that. C# also doesn't let you have a reference which points to another reference, which simplifies the syntax)ahh I see, here
*pFoo = newValue
, we are assigning a new value to where the pointer points to?Yep,
result = 20
means the compiler will look at the value of result
, go to that memory address (an operation known as dereferencing), and write the value 20
In C/C++, yesyeah I see
thanks !
In C#, you can write things like:
behind the scenes, result is a reference, when we assign 20 to result, it's like we are changing the value of the memory address at which result is, to 20, right ?
We're taking the value of
result
(which is a memory address), going to the memory with that address, and writing 20, yesyeahh, thanks !!!
will have to look at that later on, I've not yet study the ref keyword in methods
It's entirely analogous to a
ref
parameter passed to a methodhere, is it correct to say that we declare 2 variables of type int.
Then, we declare a reference that "points to" same memory address as foo, we use the int keyword because foo is an int?
Then later on, we change that reference to now points to where the reference of bar is.
Any change made to bar should reflect in the variable reference or vice-versa
ref int reference
declares a variable called reference
whose type is a reference to an int
Yep for the other stuffah ok, like we can say the reference is a memory address, "reference to an int" means, that memory address contains an int ?
It means that
reference
contains a memory address (rather than an int), and that value at that memory address is an intyeppp, thanks, my doubts have been cleared, will be back if I have other questions but must be good for now, really appreciate 👍
Great!