Even though with the .NET framework we don't have to actively worry about memory management and garbage collection (GC), we still have to keep memory management and GC in mind in order to optimize the performance of our applications. Also, having a basic understanding of how memory management works will help explain the behavior of the variables we work with in every program we write. In this article, I'll cover some of the behaviors we need to be aware of when passing parameters to methods.
In Part I, we covered the basics of the Heap and Stack functionality and where Variable Types and Reference Types are allocated as our program executes. We also covered the basic idea of what a Pointer is.
Parameters, the Big Picture
Here's the detailed view of what happens as our code executes. We covered the basics of what happens when we make a method call in Part I. Let's get into more detail...
When we make a method call here's what happens:
- Space is allocated for information needed for the execution of our method on the stack (called a Stack Frame). This includes the calling address (a pointer) which is basically a GOTO instruction so when the thread finishes running our method it knows where to go back to in order to continue execution.
- Our method parameters are copied over. This is what we want to look at more closely.
- Control is passed to the JIT'ted method and the thread starts executing code. Hence, we have another method represented by a stack frame on the "call stack".
The code:
public int AddFive(int pValue)
{
int result;
result = pValue + 5;
return result;
}
Will make the stack look like this:

NOTE : the method does not live on the stack, and is illustrated here just for reference as the beginnnig of the stack frame.
As discussed in Part I, Parameter placement on the stack will be handled differently depending on whether it is a value type or a reference type. A value types is copied over and the reference of a reference type is copied over.ed over.
Passing Value Types
Here's the catch with value types...
First, when we are passing a value types, space is allocated and the value in our type is copied to the new space on the stack. Look at the following method:
class Class1
{
public void Go()
{
int x = 5;
AddFive(x);
Console.WriteLine(x.ToString());
}
public int AddFive(int pValue)
{
pValue += 5;
return pValue;
}
}
As the method executes, space for "x" is placed on the stack with a value of 5.

Next, AddFive() is placed on the stack with space for it's parameters and the value is copied, bit by bit from x.

When AddFive() has finished execution, the thread is passed back to Go() and because AddFive() has completed, pValue is essentially "removed":

So it makes sense that the output from our code is "5", right? The point is that any value type parameters passed into a method are carbon copies and we count on the original variable's value to be preserved.
One thing to keep in mind is that if we have a very large value type (such as a big struct) and pass it to the stack, it can get very expensive in terms of space and processor cycles to copy it over each time. The stack does not have infinite space and just like filling a glass of water from the tap, it can overflow. A struct is a value type that can get pretty big and we have to be aware of how we are handling it.
Here's a pretty big struct:
public struct MyStruct
{
long a, b, c, d, e, f, g, h, i, j, k, l, m;
}
Take a look at what happens when we execute Go() and get to the DoSomething() method below:
public void Go()
{
MyStruct x = new MyStruct();
DoSomething(x);
}
public void DoSomething(MyStruct pValue)
{
// DO SOMETHING HERE....
}


难得自然萌Posted Jun 28, 2020, 9:16 PM
Go() method example is very good
Gowtham RajamanickamPosted Apr 19, 2016, 6:46 AM
very good one..
Muhammad BabarPosted Feb 23, 2016, 5:56 AM
Huge thanks.......... @Matthew Cochran stch a gud article to make understand
Flux HaloPosted Sep 26, 2013, 1:55 PM
clear and straight to the point...so easy to understand thanks for the wonderful article :D
Torsten BuschPosted Mar 28, 2013, 10:49 AM
very very well explained! thank you for that!
Fred FowlerPosted Apr 19, 2010, 2:13 PM
Continuing on with your lessons. Part 2 was as clear as Part 1. thanks again...Heading to part 3
crni konjPosted Sep 24, 2009, 10:48 AM
so passing a reference type by reference is basically a pointer to pointer?
devendra chauhanPosted Sep 14, 2007, 2:23 AM
hi matthew, Its raelly a great and hard to make it all lucid clear. i m devendra chauhan [email protected]
deepti potdarPosted Jul 9, 2007, 12:12 PM
Hi Mathew, This article is very well written and easy to understand . Thanks Regards DP
chandramani pandeyPosted Dec 2, 2006, 2:32 AM
hi Mathew!! In my last comment("Stack(ing) and heap(ing)....") i think i forget to give u my email address for further communication. so here,s mine [email protected].. bye chandramani