2

我不确定我的标题是否足够明确或具体,但这就是我想要做的。

我有一个新班级

public class Segments
    {            
        public List<double> List1 { get; set; }
        public List<double> List2 { get; set; } 
        public List<double> List3 { get; set; } 
        public List<double> List4 { get; set; } 
    }

public static void SplitSegments(CsvClass longList, List<Segments> segments)
    {
        Segments tempSegments = new Segments();
        List<double> list1 = new List<double>();
        List<double> list2 = new List<double>();
        List<double> list3 = new List<double>();
        List<double> list4 = new List<double>();

        // Nested For loop that goes through a longList with its own properties.
        // Below is a flag for when to split that longList.properties into segments

        if (flag == true)
        {
             //The lists are now complete for the first segment.
             list1.Add(longList.one[i]);
             list2.Add(longList.two[i]);
             list3.Add(longList.three[i]);
             list4.Add(longList.four[i]);

             //created a copy of the class properties
             tempSegments.List1 = new List<double>(list1);
             tempSegments.List2 = new List<double>(list2);
             tempSegments.List3 = new List<double>(list3);
             tempSegments.List4 = new List<double>(list4);

             //Add to List<Segments>
             segments.Add(tempSegments)

             //Clear lists in order to move on to creating next segment of the longList.
             list1.Clear();
             list2.Clear();
             list3.Clear();
             list4.Clear();
             break;
        }
    }
}

我的问题是,当创建新段并将其添加到 时List<Segments>,所有段都成为新段的完全相同的副本。

我想类中的列表仍然引用与列表相同的对象。我的问题是如何做到这一点,以便在将新段添加到 时List<Segments>,它们不会删除旧段?

4

2 回答 2

2

你应该移动这条线

Segments tempSegments = new Segments();

进入循环体,可能就在if子句if (flag == true)中。这将在每次迭代中创建一个新段。

整个代码部分如下所示:

    if (flag == true)
    {
         Segments tempSegments = new Segments();
         //The lists are now complete for the first segment.
         list1.Add(longList.one[i]);
         list2.Add(longList.two[i]);
         list3.Add(longList.three[i]);
         list4.Add(longList.four[i]);

         //created a copy of the class properties
         tempSegments.List1 = new List<double>(list1);
         tempSegments.List2 = new List<double>(list2);
         tempSegments.List3 = new List<double>(list3);
         tempSegments.List4 = new List<double>(list4);

         //Add to List<Segments>
         segments.Add(tempSegments)

         //Clear lists...
         list1.Clear();
         list2.Clear();
         list3.Clear();
         list4.Clear();
         break;
    }
于 2013-01-28T03:39:01.740 回答
0
//created a copy of the class properties
tempSegments = new Segments();  // otherwise you are just changing the properties on a single obj and adding that obj many times to your list.
tempSegments.List1 = new List<double>(list1);
tempSegments.List2 = new List<double>(list2);
tempSegments.List3 = new List<double>(list3);
tempSegments.List4 = new List<double>(list4);
于 2013-01-28T03:41:33.280 回答