C #. What will happen if the structure member is a reference?

Source: Internet
Author: User

As we all know, the structure (struct) is the value type, and the class (class) is the reference type. Now let's consider a question. If there is a reference type member in the structure, what will happen?
Now we have a structure oneline. Let's first introduce its features. It can be regarded as a line of string, but it also consists of several parts. Let me take an example: "ABCD", in some applications, I may need to know which elements the string consists of, whether it is A/B/C/D or ABC/d, considering that this line can be added, I used an arraylist to put these elements. I will know it when I call it later. Oh, it's ABC/d, instead of A/B/C/d, the corresponding processing is made;
Now let's look at my test code (with a debugger image ):
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 a copy when the structure is assigned to another structure, line_2 and line_3 should be the same.
I took it for granted when I was writing a program .. as shown in the figure below, when you use line_2 in the code below, if you think it only contains the element "test, errors are inevitable.
After finding out this problem, I wrote a copy method to return the copy of this structure, and the problem never occurs again. We can see from the figure that line_3 is running as expected.

Finally, let me briefly describe what I learned about this exception ..
The Structure assignment should be equivalent to the superficial replication of the class (copying a value when a member is a value, and copying a reference when a member is a reference), rather than completely copying ..
Appledotnet@hotmail.com with 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 a row
{
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.
{
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.