C
C#2mo ago
Faker

✅ 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
canton7
canton72mo ago
Do you know what a (C-style) pointer is?
Faker
FakerOP2mo ago
yeah, I know a bit I need to refresh but I know a bit how it works
canton7
canton72mo ago
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 at
Faker
FakerOP2mo ago
yeah 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
canton7
canton72mo ago
The compiler knows that it's a pointer, and does the derefencing for you
Faker
FakerOP2mo ago
ah
canton7
canton72mo ago
Other languages do the same. See e.g. C++ and & references, or rust and auto-deref
Faker
FakerOP2mo ago
yep 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)
canton7
canton72mo ago
(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)
Faker
FakerOP2mo ago
ahh I see, here *pFoo = newValue , we are assigning a new value to where the pointer points to?
canton7
canton72mo ago
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++, yes
Faker
FakerOP2mo ago
yeah I see thanks !
canton7
canton72mo ago
In C#, you can write things like:
int foo = 20;
int bar = 30;

ref int reference = ref foo;
reference = ref bar;
int foo = 20;
int bar = 30;

ref int reference = ref foo;
reference = ref bar;
Faker
FakerOP2mo ago
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 ?
canton7
canton72mo ago
We're taking the value of result (which is a memory address), going to the memory with that address, and writing 20, yes
Faker
FakerOP2mo ago
yeahh, thanks !!! will have to look at that later on, I've not yet study the ref keyword in methods
canton7
canton72mo ago
It's entirely analogous to a ref parameter passed to a method
Faker
FakerOP2mo ago
here, 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
canton7
canton72mo ago
ref int reference declares a variable called reference whose type is a reference to an int Yep for the other stuff
Faker
FakerOP2mo ago
ah ok, like we can say the reference is a memory address, "reference to an int" means, that memory address contains an int ?
canton7
canton72mo ago
It means that reference contains a memory address (rather than an int), and that value at that memory address is an int
Faker
FakerOP2mo ago
yeppp, thanks, my doubts have been cleared, will be back if I have other questions but must be good for now, really appreciate 👍
canton7
canton72mo ago
Great!

Did you find this page helpful?