public myTestClass deepCopy() { myTestClass tempObject = new myTestClass(); //composes a new instance of object from myTestClass tempObject.irTestVal = this.irTestVal; //value type tempObject.srName = this.srName; //immutable tempObject.srLocation = this.srLocation; //immutable tempObject.lstCollection = this.lstCollection.ToList(); //i am making a deep copy here with ToList method call return(tempObject); }
private void btnShallowCloning_Click(object sender, RoutedEventArgs e) { List <int> lstSecondList = lstMyMainList; lstSecondList[2] = 400; //the below foreaches are equal, therefore they will throw error //collection is modified during enumaration error //foreach (var vrSecondList in lstSecondList) //{ // lstMyMainList.Add(vrSecondList); //} //foreach (var vrSecondList in lstMyMainList) //{ // lstMyMainList.Add(vrSecondList); //} //when assign reference type objects it just copies their address //any changes on them modified the same data myTestClass test = new myTestClass(); test.irTestVal = 100; myTestClass test2 = test; test2.irTestVal = 200; //now both test and test2 irTestVal is 200 int ir1 = 100; int ir2 = ir1; ir1 = 200; //now ir1 = 200 and ir2= 100 because these are value types string sr1 = "hello world"; string sr2 = sr1; sr1 = "hello home"; //now sr1 is hello home and sr2 is hello world //string is only exception of shallow copy of class type //because //Strings in C# are immutable; that means they cannot change. When you say s = "hello home" you are creating a new string and assigning its reference to sr1; this does not affect the reference saved to sr2 which still points to the original string. //since string is immutable, whenever you concatenate a string, it composes a new string. therefore, for long strings you should use stringbuilder }
private void btnDeepCopy_Click(object sender, RoutedEventArgs e) { List <int> lstSecondList = lstMyMainList.ToList(); lstSecondList[2] = 400; //this will work since they are different lists now foreach (var vrSecondList in lstSecondList) { lstMyMainList.Add(vrSecondList); } myTestClass myDefaultTestClass = new myTestClass(); myTestClass myTest2 = new myTestClass { irTestVal = 250, lstCollection = new List <int> { 500, 1000, 10 }, srLocation = "Turkey", srName = "Toros University" }; //how can i make a deep copy of myTest2 into myDefaultTestClass?? myDefaultTestClass = myTest2;// how it copies? this is only shallow cloning myDefaultTestClass.lstCollection[2] = 1; myTest2.lstCollection[1] = 3; myDefaultTestClass.srLocation = "default location of myDefaultTestClass"; myTest2.srLocation = "default location of myTest2"; myDefaultTestClass = myTest2.deepCopy(); myDefaultTestClass.lstCollection[2] = 800; myTest2.lstCollection[1] = 5000; myDefaultTestClass.srLocation = "new location of myDefaultTestClass"; myTest2.srLocation = "great location of myTest2"; myTestClass test3 = myTest2.DeepClone(); myTest2.lstCollection[2] = 1800; test3.lstCollection[1] = 15000; myTest2.srLocation = "awesome location of myDefaultTestClass"; myTest2.srLocation = "super location of myTest2"; }