In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "detailed introduction of deep and shallow copies of C#". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Shallow copy
First of all, let's look at the shallow copy. Shallow copy is to copy all the fields in the object to the new object, and shallow copy has different effects on the value type and reference type. After the value of the value type is copied to the copy, modifying the value in the copy does not affect the value of the original object. However, it is the reference of the reference type that is copied to the copy. Is not a referenced object. In this way, modifying the value in the copy will cause the value of the original object to be modified. But here we need to exclude the string String type in the case of the reference type.
So why does the value of the modified copy of the reference type result in a change in the value of the original object, while the string string type is excluded? First of all, we need to know the concept that the string type is an immutable data type, which means that the string object cannot be changed once the string object is initialized. On the surface, our method and operation to modify the contents of a string is actually to create a new string, and then copy the contents of the old string to the new string as needed. How to understand you? Let's look at the following example:
# region string comparison / public static string getMemory (object o) {GCHandle h = GCHandle.Alloc (o, GCHandleType.Pinned); IntPtr addr = h.AddrOfPinnedObject (); return "0x" + addr.ToString ("X");} / string comparison / public static void Compares () {string a = "123" Console.WriteLine (citation address of "a:\ t\ t" + getMemory (a)); string b = "123"; Console.WriteLine ("citation address of b:\ t\ t" + getMemory (b)); Console.WriteLine ("comparison of an and b:\ t\ t" + Object.ReferenceEquals (a, b)); b = "456"; Console.WriteLine ("citation address of b:\ t\ t" + getMemory (b);} # endregion
Here we see a = "123", b = "123". We see that their quotation address is the same. That is to say, when we first create a, we create the string an and have a reference address. Then when we create b, we first look for the existence of the same value. If the same value exists, get its reference address. This is why the reference addresses of an and b are the same. This involves something called the character resident pool. The string is saved. Then we modify the value of b and then output its reference address, which is different from the previous reference address. The description is not to modify the original value, but to recreate a string and retrieve its reference address.
Let's take a look at a shallow copy case. First of all, we prepare the values of the following data types: int,string,enum,struct,class,int [], string [].
/ enumerate / public enum EnumTest {TestOne = 1, TestTwo = 2} / structure / public struct StructTest {public int Test; public StructTest (int I) {Test = I;}} / Class / public class ClassTest {public string TestString; public ClassTest (string _ string) {TestString = _ string } / Deep copy / public class DeepClone: ICloneable {public int _ int = 1; public string _ string = "1"; public EnumTest _ enum = EnumTest.TestOne; public StructTest _ struct = new StructTest (1); public ClassTest _ class = new ClassTest ("1"); public int [] arrInt = new int [] {1}; public string [] arrString = new string [] {"1"}; public object Clone () {var NewOne = JsonConvert.SerializeObject (this) Return JsonConvert.DeserializeObject (NewOne);}} class Program {static void Main (string [] args) {DeepClone simple = new DeepClone (); var simpleTwo = (DeepClone) simple.Clone (); simpleTwo._int = 2; simpleTwo._string = "2"; simpleTwo._enum = EnumTest.TestTwo; simpleTwo._struct.Test = 2; simpleTwo._class.TestString = "2"; simpleTwo.arrInt [0] = 2; simpleTwo.arrString [0] = "2" Console.WriteLine ($"int type change original object: {simple._int}\ t backup object: {simpleTwo._int}"); Console.WriteLine ($"string type change original object: {simple._string}\ t\ t backup object: {simpleTwo._string}"); Console.WriteLine ($"enum type change original object: {(int) simple._enum}\ t\ t backup object: {(int) simpleTwo._enum}") Console.WriteLine ($"struct type change original object: {simple._struct.Test}\ t\ t backup object: {simpleTwo._struct.Test}"); Console.WriteLine ($"class type change original object: {simple._class.TestString}\ t\ t backup object: {simpleTwo._class.TestString}") Console.WriteLine ($"int array type change original object: {simple.arrInt [0]}\ t\ t backup object: {simpleTwo.arrInt [0]}"); Console.WriteLine ($"string array type change original object: {simple.arrString [0]}\ t\ t backup object: {simpleTwo.arrString [0]}");}}
We make a shallow copy of these types by inheriting the ICloneable interface and then modify the copy object. Output the original object and the copy object for comparison. We found that the original object values of int,enum,struct, value types, and string, a special reference type, were not affected. But class,int [], string [] these reference type objects are affected to change the value of the original object. It once again verifies what we said earlier. Shallow copy is to assign an object to a copy object, the value type copies the value, and the reference type copies its reference object. Modify the value of the copy object, the value type and the string original object will not be affected to change, the reference type except string its original object will be affected.
Deep copy
We have seen the shallow copy above, the shallow copy still has a certain impact, if it is not handled well, it may become bug. So let's see what the corresponding deep copy looks like. It can be declared here that deep copies make no difference between value types and reference types. Deep copy also copies all fields in the object to the new object, but both the value type and the reference type of the object will be recreated and copied to the replica object. Modifications to the copy object will not affect the original object, regardless of type.
Let's continue to make a deep copy of the above example:
/ Deep copy / public class DeepClone: ICloneable {public int _ int = 1; public string _ string = "1"; public EnumTest _ enum = EnumTest.TestOne; public StructTest _ struct = new StructTest (1); public ClassTest _ class = new ClassTest ("1"); public int [] arrInt = new int [] {1}; public string [] arrString = new string [] {"1"}; public object Clone () {var NewOne = JsonConvert.SerializeObject (this); return JsonConvert.DeserializeObject (NewOne) }} class Program {static void Main (string [] args) {DeepClone simple = new DeepClone (); var simpleTwo = (DeepClone) simple.Clone (); simpleTwo._int = 2; simpleTwo._string = "2"; simpleTwo._enum = EnumTest.TestTwo; simpleTwo._struct.Test = 2; simpleTwo._class.TestString = "2"; simpleTwo.arrInt [0] = 2; simpleTwo.arrString [0] = "2" Console.WriteLine ($"int type change original object: {simple._int}\ t backup object: {simpleTwo._int}"); Console.WriteLine ($"string type change original object: {simple._string}\ t\ t backup object: {simpleTwo._string}"); Console.WriteLine ($"enum type change original object: {(int) simple._enum}\ t\ t backup object: {(int) simpleTwo._enum}") Console.WriteLine ($"struct type change original object: {simple._struct.Test}\ t\ t backup object: {simpleTwo._struct.Test}"); Console.WriteLine ($"class type change original object: {simple._class.TestString}\ t\ t backup object: {simpleTwo._class.TestString}") Console.WriteLine ($"int array type change original object: {simple.arrInt [0]}\ t\ t backup object: {simpleTwo.arrInt [0]}"); Console.WriteLine ($"string array type change original object: {simple.arrString [0]}\ t\ t backup object: {simpleTwo.arrString [0]}");}}
Here we look at the run result. Neither the value type nor the reference type changes the value of the original object after modifying the copy object. This is the characteristic of deep copy.
Summary
After we have seen the shallow copy and the deep copy, let's review it carefully. Shallow copy copies the fields of the object to the new object, but when you modify the new object, the fields of the value type and string type will not affect the fields of the original object, while the reference type will affect the value of the original object except the string type. Deep copy also copies the fields of the object to the new object, but changes in either the value type or the reference type do not affect the value of the original object. Because the deep copy is to recreate the original object and copy it into the replica object.
This is the end of the detailed introduction of the deep and shallow copy of C#. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.