problems with list in C #

Source: Internet
Author: User

Problem Description:

A list that holds struct, and if the struct data is added to another list again,

Changing the value in the struct in the second list will affect the first list.

Now use the code to prove:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;


Using System.Data;
        Namespace CSharp {class Program {public int a = 1;
        public int b = 2;
            Internal struct Info {public int c;
        public int D;
                     

        public static void Main (string[] args) {ListCopy2 ();
            public static void ListCopy2 () {//First list, element type struct, internally containing two cosmetic values
            list<info> ls = new list<info> ();
            Info info = new info ();
            Initializes the two values info.c = 2;
            INFO.D = 3; Add this struct to the list of LS.

            ADD (info);  
            The second list, which has the same element type as the element type in the first list list<info> LS2 = new list<info> (); Adds the first element in the first list to the second list LS2.
            ADD (Ls[0]);

 Remove this value from the second list and reassign Info Info2 = ls2[0];           The assignment is 4 info2.c = 4;
            Ls2[0] = Info2;
            Print View effect Console.WriteLine (LS[0].C);
            Console.WriteLine (LS2[0].C);

        Console.readkey ();
 }
        
    }
}



Experimental results:

The results show that there is no mutual influence.

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.