C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Assigning One Array Reference Variable To Another.


Posted Date: 03 Jun 2004    Resource Type: Articles    Category: .NET Framework
Author: ANITA MARY JOSEPHMember Level: Gold    
Rating: 1 out of 5Points: 5



Assigning Array References
When you assign one array reference variable to another, you are simply changing the object to which the variable refers. You are not causing a copy of the array to be made, nor are you causing the contents of one array to be copied to the other.

For example consider the following program:

// Assigning array reference variables.
using System;

class AssignReference
{
public static void Main()
{
int i;
int[] array1 = new int[10];
int[] array2 = new int[10];

for(i =0; i < 10; i++)
array1[i] = i;
for(i =0; i < 10; i++)
array2[i] = -i;

Console.Write("Contents of array1: ");
for(i =0; i < 10; i++)
Console.Write(array1[i] + " ");
Console.WriteLine();

Console.Write("Contents of array2: ");
for(i =0; i < 10; i++)
Console.Write(array2[i] + " ");
Console.WriteLine();

array2 = array1; // now array2 refers to array1

Console.Write("Contents of array2 After Assignment: ");
for(i =0; i < 10; i++)
Console.Write(array2[i] + " ");
Console.WriteLine();

// now lets operate on array1 through array2
array2[4] = 82;

Console.Write("Contents of array1 after the change through array2: ");
for(i =0; i < 10; i++)
Console.Write(array1[i] + " ");
Console.WriteLine();

Console.Read();

}
}


The output from the program is shown here:

Contents of array1: 0 1 2 3 4 5 6 7 8 9
Contents of array2: 0 -1 -2 -3 -4 -5 -6 -7 -8 -9
Contents of array2 After Assignment: 0 1 2 3 4 5 6 7 8 9
Contents of array1 after the change through array2: 0 1 2 3 82 5 6 7 8 9

As the output shows, after the assignment of array1 to array2, both array reference variables refer to the same object.



Responses

Author: S. Senthil Kumar    20 Jun 2004Member Level: Bronze   Points : 0
Arrays are first class objects, so why treat them as a special case? Normally, when you assign a object (reference) to another, only the reference gets changed, not the contents.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How To Call ActiveX DLL in .Net Environment
Previous Resource: Create XML file using C# .NET (with indentation)
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use