首页 > 解决方案 > 如何返回已在方法内修改的对象?

问题描述

我正在使用 C#、Visual Studio 2017 和 .NET Framework 4.7.1 进行开发。

我的程序做了很多计算,我不想创建新的集合来存储它们。所以,我创建了一个集合来重用它(我唯一要做的就是更改集合的内容):

List<List<double>> offsprings;

在以下方法中,我将清除offsprings参数的内容以不创建新的List<double>

public void GlobalRecombination(
    List<List<double>> population,
    List<List<double>> offsprings,
    int numOfObjectVariable,
    int numOfStrategyParameters,
    RecombinationType objVarRecomType,
    RecombinationType straParamRecomType)
{
    foreach (List<double> offspring in offsprings)
    {
        offspring.Clear();

        for (int i = 0; i < numOfObjectVariable; i++)
        {
            double newValue = 0.0;

            [ ... ]

            offspring[i] = newValue;
        }

        int total = numOfObjectVariable + numOfStrategyParameters;
        for (int i = numOfObjectVariable; i < total; i++)
        {
            double newValue = 0.0;

            [ ... ]

            offspring[i] = newValue;
        }
    }
}

我的问题是:返回offspring参数的最佳方式是什么?

现在,该方法有效,但我不确定这是否是正确的方法。

标签: c#software-design

解决方案


推荐阅读