Note from the internet

Shallow Copy and Deep Copy

A struct in C# automatically implements a memberwise copy, sometimes known as a shallow copy. The object root class has a protected method, MemberwiseClone, which will perform a memberwise copy of members of a class.

If one or more members of a class are of a reference type, this memberwise copy may not be good enough. The result will be two references to the same data, not two independent copies of the data. To actually copy the data itself and not merely the references, you will need to perform a deep copy. Deep copy can be provided at either the language level or the library level. In C++, deep copy is provided at the language level through a copy constructor. In C#, deep copy is provided by the .NET Framework through a special interface, ICloneable, which you can implement in your classes in order to enable them to perform deep copy.

 

using System;
//csc array_shallowcopy.cs
namespace shallowcopy
{
class test
{
public static void Main()
{
string[ , ] a = new string[2, 2];
string[] b = {"a","b","c","d"};
string[] c = b; //simple copying is shallow copy7
//both the arrays will refer to same address
int [] x = {1,2,3,4,5};
Console.WriteLine("---shallow copy---");
foreach(string s in c){ Console.Write(s + " " );}
Console.WriteLine();
Console.WriteLine("lenght of b = {0}",b.Length);
for(int i=0; i<x.Length; i++) {Console.Write("{0}{1}", x[i], " : ");}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("a: " + a.Rank );
Console.WriteLine("b: " + b.Rank );
Console.ReadLine();

}
}
}

 

using System;
//csc array_shallowcopy_b.cs
namespace shallowcopy
{
class test
{
public static void Main()
{
string[ , ] a = new string[2, 2];
string[] b = {"a","b","c","d"};
string[] c = b; //simple copying is shallow copy
//both the arrays will refer to same address
int [] x = {1,2,3,4,5};
Console.WriteLine("---shallow copy---");
foreach(string s in c){ Console.Write(s + " " );}
Console.WriteLine();
Console.WriteLine("lenght of b = {0}",b.Length);
for(int i=0; i<x.Length; i++) {Console.Write("{0}{1}", x[i], " : ");}
Console.WriteLine();
c[1] = "m";
b[3]="z";

Console.WriteLine();
foreach(string s in b){ Console.Write(s + " " );}
Console.WriteLine();
foreach(string s in c){ Console.Write(s + " " );}
Console.WriteLine();
Console.WriteLine("a: " + a.Rank );
Console.WriteLine("b: " + b.Rank );
Console.ReadLine();

}
}
}

When you change any index of these arrays, it will change either of the arrays both source and copied one.
 

array_shallowcopy_c.cs

using System;
//csc array_shallowcopy_c.cs
namespace shallowcopy
{
public class simple
{
static int n1 = 0;
public string process(string[] s1)
{
//Console.WriteLine();
s1[3]="x";
foreach(string s in s1)
{
//if(s1[n1]="a"){ s1[n1]= "x";}
n1++;
Console.Write(s + " ");
}
return "good job";
}
}
class test
{
public static void Main()
{
string[ , ] a = new string[2, 2];
string[] b = {"a","b","c","d"};
string[] c = b; //simple copying is shallow copy
//both the arrays will refer to same address
int [] x = {1,2,3,4,5};
Console.WriteLine("---shallow copy---");
foreach(string s in c){ Console.Write(s + " " );}
Console.WriteLine();
Console.WriteLine("lenght of b = {0}",b.Length);
Console.WriteLine();
c[1] = "m";
b[3]="z";
Console.WriteLine("--note changes in b or c---");
foreach(string s in b){ Console.Write(s + " " );}
Console.WriteLine();
foreach(string s in c){ Console.Write(s + " " );}
Console.WriteLine();
//create an instace of the class
simple smp = new simple();
Console.WriteLine("--passing array to a routine-- 4th element of 0-3 will be altered--");
Console.WriteLine(smp.process(c));
Console.WriteLine("--changes in - 4th element affectd c[]--");
foreach(string s in c){ Console.Write(s + " " );}
Console.WriteLine();
Console.WriteLine("a: " + a.Rank );
Console.WriteLine("b: " + b.Rank );
Console.ReadLine();
}
}
}