阵列克隆&复制静止效果原件

时间:2013-06-21 11:40:29

标签: c# arrays copy clone

我传递了一个我想要UNTOUCHED的对象数组但是出于某种原因无论我尝试什么,传入的原始数组都被更改。

    private DSPPositionPeriodDO[] CompactShortTermData(DSPPositionPeriodDO[] data)
    {
        DSPPositionPeriodDO[] resultArray = (DSPPositionPeriodDO[])data.Clone();
        int? hoursToAggBy = null;

        // it is short term, the data gets too much so decide how many
        // hours to aggregate by.
        var days = (_endDate - _startDate).TotalDays;

        if (days <= 3)
            hoursToAggBy = null;
        else if (days > 3 && days <= 7)
            hoursToAggBy = 1;
        else if (days > 7 && days <= 14)
            hoursToAggBy = 2;
        else if (days > 14 && days <= 21)
            hoursToAggBy = 3;
        else if (days > 21)
            hoursToAggBy = 4;

        if (hoursToAggBy != null)
        {
            // round down hours
            foreach (var posPeriod in resultArray)
            {
                var sl = posPeriod.StartLocal;
                sl = sl.AddHours(-(sl.Hour % hoursToAggBy.Value));
                sl = sl.AddMinutes(-sl.Minute);
                posPeriod.StartLocal = sl;
            }

            // group by entity & time -- get average
            var groupedData = resultArray.GroupBy(x => new
            {
                x.EntityId,
                x.EntityCode,
                x.StartLocal
            })
            .Select(g => new DSPPositionPeriodDO
            {
                EntityId = g.Key.EntityId,
                EntityCode = g.Key.EntityCode,
                StartLocal = g.Key.StartLocal,
                Volume = g.Average(v => v.Volume),
                AverageVolume = null
            });

            resultArray = groupedData.ToArray();
        }

我试图创建一个单独的副本,如:

var x = original.ToArray() .. toList() ......克隆......

original我的意思是data对象被传入。我希望它不会被改变。

1 个答案:

答案 0 :(得分:1)

来自Array.Clone

  

新数组中的引用指向原始数组中的引用所指向的相同对象。

您需要将每个对象克隆到新数组中。