Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
What is Memory usage difference between  workMethods(ref myArray)  vs  workMethods(myArray) ?


What is difference between this two method calls regarding memory usage ??

C#
int[] myArray = new myArray[2000];

for(int i=0; i < myArray.Length; i++)
{
 myArray[i] = i;
}


then Pass this array to 2 methods which take int type array as parameter for process

C#
workMethods(ref myArray);


and this method

C#
workMethods(myArray);
Posted
Updated 15-Jun-14 21:35pm
v3

1 solution

Nothing.

The array is a reverence type - they always are - so all that is every passed into the method is a reference - which is pretty small (4 or 8 bytes depending on OS size)

The only difference when using the ref keyword is whether the variable holding the reference is passed through, or a copy of it. In the first case, any changes to the variable will be reflected in the world outside the method, without the ref they won't.
In both cases, any changes to the values in the array will affect the one and only copy of the actual data in existence.
 
Share this answer
 
Comments
CPallini 16-Jun-14 4:25am    
5.
Kornfeld Eliyahu Peter 16-Jun-14 4:29am    
Can I fix typo or will you shout at me? :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900