Recently, I had a dispute with co-worker about .NET passing parameter. So, I decide to write post about it and clarify some aspects. This post will show you how parameter passing is handled in memory.
Also this post expects that readers knew what's the difference between a value-type and a reference-type in .NET. By the way, I'm going to dive deeper into this question. Thus, wait for another post :)
In .NET we may passing parameter by value or by reference.
I break out a post into four parts :
- Passing value-type variables by value;
- Passing value-type variables by reference;
- Passing reference-type by value;
- Passing reference-type by reference.
Passing value-type variables by value
In this case, you are passing parameter in general way without any additional keywords.
After calling DoSomething parameter equals 10.
Look at image :
From method Main I'm calling method DoSomething and passing parameter variable as argument. In .NET local variables are placed on the stack. So, you can see that after calling DoSomething method, in stack allocated a new place for x variable and its value is coping from parameter variable.Thus, if I changed value of x variable into DoSomething method, it doesn't affect parameter variable.
After calling DoSomething parameter equals 10.
Look at image :
Passing value-type variables by reference
If you want to pass variables by reference you must use a special C# keyword - ref.Let's change previous example :
I added keyword ref to DoSomething's signature and before passing parameter variable.
Let's see whats going on the stack (look at image on the right).
On the top of the stack you can see x : POINTER.
POINTER is basically as GOTO instruction.
So now, if you change the value of x you also change the value of parameter variable.
That's why on console window you see 5 (not 10).
Passing reference-type variables by value
Passing reference-type by value is similar to passing value-type by reference.
For example, you have classes such as:
And now, you want to execute something like this :
Take a look at image on the right. Here what's happens:
- Starting with the Main method the variables human goes on the stack.
- Starting with the call to DoSomething() the argument x goes on th stack.
- The value of human (the address of Boy on the stack) is copied to x.
So it makes sense that when we change Age property of Boy object on the heap using x variable(DoSomething method) and later refer to the object on the heap using human variable, we get the value of Age equals 5.
Passing reference-type variables by reference
And the last approach that you can use to pass parameters between methods.
For example, you want to execute this:
For example, you want to execute this:
On the console window you'll see :
human is Boy : FalseLet's take a look at what's happening:
human is Girl : True
- Starting with Main method the human pointer goes on the stack;
- The Boy goes on the heap;
- Starting with DoSomething method the x goes on the stack and points to human;
- The Girl goes on the heap;
- The value of human is changed through x to the address of the Girl.
If you don't pass Human by ref you'll get the opposite results from this code...
No comments:
Post a Comment