Using an Array as a Parameter in C#

This article is about passing a reference type (like an array) to a method. Whatever I have written below is purely based on my experience and the knowledge I gained from MSDN etc. So, if somebody reading this article has a better idea then please comment. Before I write something, let us quickly look at the following 3 snippets of code. For a quick demo I have used a Console Application but, the concept below also applies to ASP.Net or Windows Applications or any other. As C# is the most advanced and most used programming language, I have decided to use it for my demonstration.
Have a look at the code.
Snippet 1    

  1. //Demonstration with Integer parameter  
  2.     static void Main(string[] args)  
  3.      {  
  4.          int num = 1000;   
  5.          Console.WriteLine("Initial Value in num: {0}", num);  
  6.          Console.WriteLine("______________________________");  
  7.          PlusPlus(num);  
  8.          Console.WriteLine("______________________________");  
  9.          Console.WriteLine("Final Value in num: {0}", num);  
  10.          Console.ReadLine();  
  11.      }   
  12.      static void PlusPlus(int numFormal)  
  13.      {  
  14.          numFormal += 1;  
  15.          Console.WriteLine("Value in numFormal: {0}", numFormal);  
  16.      }  
Output Of Snippet 1

 Initial Value in num: 1000
 Value in numFormal: 1001
 Final Value in num: 1000
Snippet 2
  1. //Demonstration with Array(Int) parameter  
  2.     static void Main(string[] args)  
  3.      {  
  4.          int[] arrNum = { 1000, 2000, 3000 };  
  5.          for(int i=0;i   
  6.          {  
  7.              Console.WriteLine("Initial Value in arrNum["+i+"] : {0}", arrNum[i]);  
  8.          }  
  9.          Console.WriteLine("_________________________________________");  
  10.          PlusPlus(arrNum);  
  11.          Console.WriteLine("_________________________________________");  
  12.          for (int i = 0; i < 3; i++)  
  13.          {  
  14.              Console.WriteLine("Final Value in arrNum["+i+"] : {0}", arrNum[i]);  
  15.          }  
  16.           Console.ReadLine();  
  17.      }  
  19.      static void PlusPlus(int[] arrNumFormal)  
  20.      {  
  21.          for (int i = 0; i < 3; i++)  
  22.          {  
  23.             arrNumFormal[i] += 1;  
  24.             Console.WriteLine("Value in arrNumFormal["+i+"]: {0}", arrNumFormal[i]);  
  25.          }  
  26.      }  

Output Of Snippet 2

 Initial Value in arrNum[0]: 1000
 Initial Value in arrNum[1]: 2000
 Initial Value in arrNum[2]: 3000
 Value in arrNumFormal[0]: 1001
 Value in arrNumFormal[1]: 2001
 Value in arrNumFormal[2]: 3001
 Final Value in arrNum[0]: 1001
 Final Value in arrNum[1]: 2001
 Final Value in arrNum[2]: 3001
Snippet 3

  1. //Demonstration with string parameter  
  2.    static void Main(string[] args)  
  3.     {  
  4.         string str = "original string";  
  5.         Console.WriteLine("Initial Value in str: {0}", str);  
  6.         Console.WriteLine("______________________________");  
  7.         ChangeString(str);  
  8.         Console.WriteLine("______________________________");  
  9.         Console.WriteLine("Final Value in str: {0}", str);  
  10.         Console.ReadLine();  
  11.     }  
  13.     static void ChangeString(string strFormal)  
  14.     {  
  15.         strFormal = "changed string";  
  16.         Console.WriteLine("Value in strFormal: {0}", strFormal);  
  17.     }  
Output Of Snippet 3

 Initial Value in str: original string
 Value Of numFormal: changed string
 Final Value Of num: original string
Analysis of Snippet 1 & its result

In the Snippet no. 1, I have declared a local integer variable 'num' and assigned a value 1000 to it. Then after displaying ("Initial Value in num: 1000") it on the screen I called a method PlusPlus(), which takes a single argument of type int. To this method I passed the variable 'num'. The body of the static method PlusPlus() consists of only 2 lines of code. The first line increments the formal parameter by 1 and then the second line of code prints ("Value in numFormal: 1001") it on the screen. Then again I am printing ("Final Value in num: 1000") the value held by the variable 'num'.
The thing to notice here is that after passing the variable to the method PlusPlus() and there although I am incrementing it by 1, it did not change its original value. This is because when I am passing a int variable to a method, exactly I am passing the value held by the variable since an int is a value type and the method computes using the value passed to it, which is held by the formal parameter, not the actual parameter.
Analysis of Snippet 2 & its result

I have repeated the same process as in the Snippet 1 except I have changed the local variable integer to a integer array. But, here the result is something different. The difference is that the changes I have done to the formal parameter in the method PlusPlus() also reflects for the actual parameter or, the original integer array declared in the main() method. This happens since an array is a reference type so, when I am passing an array to a method, actually I am passing its reference and now the formal parameter holds the same reference as the actual parameter. So, when I am changing the value present in any of the indices of the array in the formal parameter it reflects in the actual parameter also.
Analysis of Snipet 2 & its result
Once again I have repeated the same process as snippet 1 and 2 with the same little change in the datatype of the variable. Now, I want to see the result when I am using a string variable, which is of reference type. But the result is the same as from the snippet 1 where I had used an integer variable.
Now, Question is : why this deviation ?
This happened because, although a string is a reference type but the class System.String or, its alias string is immutable. If you want to know more on immutable type you can search it in MSDN. I will discuss mutable and immutable types in details in my next article but for now, you can consider the real meaning of the English word 'immutable' - 'Not susceptible to change'. It means we cannot change the value held by a string variable. When we do change the value held by the string variable in the method ChangeString(), actually a new variable is created to store the new value. So, the changes did not reflect the actual parameter or, the original variable.
When we are passing an array as a parameter we should understand that, the calling method can change its value.

Similar Articles