Everyone knows. Structure is a value type, class (class) is a reference type, now we consider a question, if the structure has a reference type member, what is the situation? Now we have a structure OneLine, first Introducing its characteristics, it can basically be seen as a string, but it is made up of several parts, I will take an example: "abcd", in some applications, I may need to know this string Which elements are constituted, A / B / C / D is still ABC / D, and considering this line can be added, so I use an arraylist to put these elements, when calling, I know, oh, it is ABC / D, not A / B / C / D, thus making corresponding processing; now, let's see my test code (attached to the test): OneLine line_1 = new oneLine ("TEST "); OneLine line_2 = line_1; oneLine line_3 = line_1.copy (); line_1.add (" new_line "); return; if the reference type in the structure is replicated to another structure when the structure is assigned to another structure, Then line_2 and line_3 should be the same. I just wrote a program when I wrote a program. The result is a very crazy exception, see the picture, if you use Line_2 in the code below, you think There is only one element of "test", the error is inevitable. I have found this problem, I wrote a copy method to return to this structure, the problem will never appear, you can see it from the picture At, Line_3 is running in accordance with our expectations.
Finally, simply express the exception today to learn .. The assignment of the structure should be equivalent to the shallow copy of the class (the member is a value, and the member is a reference to copy the reference), not completely copy. .Appledotnet @ hotmail.com 2004/07/08 with oneLine structural code
Public struct one orient {intlene; string line; arraylist al; spread line // initialization {this.Line = line; this.Length = line.length; this.al = new arraylist (); this.al. Add (} public bool add (string newline) // increases a line {Foreach (Object Obj IN AL) = == newline) Return False; this.al.add (newline); this .Line = this.Line newline; this.Length = this.Line.LENGTH; Return True;} Override public string toString () {return line;} public one or/} // Return a copy copy {OneLine OL = New OneLine (this.al [0]. Tostring ()); for (int i = 1; i