C #, struct members are references, what happens

Source: Internet
Author: User
Tags reference shallow copy tostring
You know. struct (struct) is a value type, and Class (class) is a reference type, and now we consider a problem, what happens if the struct has members of a reference type?
Now we have a structure oneline, to introduce its characteristics, it can be regarded as a line of string, but at the same time it is composed of several parts, I quote: "ABCD", in some applications, I may need to know which elements of this string is composed of, is a/b/c/ D or ABC/D, and considering that this line can be added, so I used a ArrayList to put these elements, later called, I know, oh, is abc/d, rather than a/b/c/d, so as to make the corresponding treatment;
Now, let's look at my test code (with the debugger picture):
Oneline line_1=new oneline ("test");
Oneline line_2=line_1;
Oneline line_3=line_1.copy ();
Line_1.add ("New_line");
Return
Line_2 and line_3 should be the same if the reference type in the structure is to copy a copy when the struct is assigned to another structure.
I took it for granted when I was writing a program. The result is a very crazy exception, see figure, for example, when you use line_2 in the following code, if you think it has only "test" this element, the error is inevitable.
Finding out the problem, I wrote a copy method to return the copy of the structure, and the problem never appeared, as we can see from the graph that Line_3 is running as we expected.

Finally, briefly describe what this anomaly has taught me today.
The assignment of a struct should be a shallow copy equivalent to a class (a member is a value that duplicates a value, a member is a reference to copy a reference), not a full copy.
Appledotnet@hotmail.com 2004/07/08 attached oneline structure code see later



public struct Oneline
{
int Length;
String line;
ArrayList AL;
Public Oneline (string line)//initialization
{
This. Line=line;
This. Length=line.length;
This. Al=new ArrayList ();
This. AL. ADD (line);
}
public bool Add (string newline)//Add one line
{
foreach (Object obj in AL)
if (((string) obj) ==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 oneline Copy ()//Returns a copy of the replica
{
Oneline ol=new Oneline (this. Al[0]. ToString ());
for (int i=1;i< AL. count;i++)
Ol. ADD (Al[i]. ToString ());
return OL;
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.