Flyinsky

志在四方,浪迹天涯

导航

be careful of array reference type

Recently I am reading  the C# Reference in the VS2005 help.

It is very efficient for me to solidify my basic knowledge about C#.

I recorded something that I didn't attend to, today is about reference types.

using System;
class PassingRefByVal 
{
   
static void Change(int[] arr)
   
{
      arr[
0]=888;   // This change affects the original element.
      arr = new int[5{-3-1-2-3-4};   // This change is local.
      Console.WriteLine("Inside the method, the first element is: {0}", arr[0]);
   }


   
public static void Main() 
   
{
      
int[] myArray = {1,4,5};
      Console.WriteLine(
"Inside Main, before calling the method, the first element is: {0}", myArray [0]);
      Change(myArray);
      Console.WriteLine(
"Inside Main, after calling the method, the first element is: {0}", myArray [0]);
   }

}

the result is
Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: 888

the array, myArray, which is a reference type, is passed to
the mothod without the ref parameter.In such a case, a copy of the reference,
which points to myArray, is passed to the method.
The output shows that it is possible for the method to change the contents of an array element (from 1 to 888).
However, allocating a new portion of memory by using the new operator inside the Change method makes the
variable arr reference a new array. Thus, any changes after that will not affect the original array, myArray,
which is created inside Main. In fact, two arrays are created in this example, one inside Main and one inside the Change method.

If we use the ref keyword like below

using System;
class PassingRefByRef 
{
   
static void Change(ref int[] arr)
   
{
      
// Both of the following changes will affect the original variables:
      arr[0]=888;
      arr 
= new int[5{-3-1-2-3-4};
      Console.WriteLine(
"Inside the method, the first element is: {0}", arr[0]);
   }


   
public static void Main() 
   
{
      
int[] myArray = {1,4,5};
      Console.WriteLine(
"Inside Main, before calling the method, the first element is: {0}", myArray [0]);
      Change(
ref myArray);
      Console.WriteLine(
"Inside Main, after calling the method, the first element is: {0}", myArray [0]);
   }

}

we will get another result
Inside Main, before calling the method, the first element is: 1
Inside the method, the first element is: -3
Inside Main, after calling the method, the first element is: -3

Let me summarize.
Variables of reference types, referred to as objects, store references to the actual data. This section introduces the following keywords used to declare reference types:

  • class
  • interface
  • delegate

This section also introduces the following built-in reference types:

  • object
  • string

It is eery string is also a reference type, oh , in .NET string is also a class I think it should be the reason.

posted on 2004-09-22 16:26  flyinsky  阅读(653)  评论(1编辑  收藏  举报