C#为什么我在方法调用期间得到最后一个索引值?

时间:2017-03-14 23:08:14

标签: c#

我正在调用CalcGamma方法,它在CalcResult中返回一个double值。在调试时,它会计算每个列表的double值。在我调试时,我注意到在CalcGamma方法的return语句中,它只返回并分配最后一个索引的值。我在这里做错了什么?

public GammaCalcResult CalculateGamma(GammaCalcParam param)
{
    var resultPortfolios = new List<PortfolioOutputModel>();
    foreach (var portfolio in param.portfolios)
    {
        resultPortfolios.Add(new PortfolioOutputModel
        {
            portfolioIdentifier = portfolio.portfolioIdentifier,
            portfolioGamma = CalcGamma(param.portfolios, param.phases)
        });
    }

    return new GammaCalcResult
    {
        portfolios = resultPortfolios
    };
}

private double CalcGamma(List<PortfolioInputModel> portfolios, List<PhaseInputModel> phases)
{
    double currentGamma = 0.00;
    List<List<double>> intermediate_gamma_calc1 = gammaFunc(portfolios, phases);
    for (int i = 0; i < intermediate_gamma_calc1.Count; i++)
    {
        double val = 0;
        for (int j = 0; j < intermediate_gamma_calc1[0].Count; j++)
        {
            val += (intermediate_gamma_calc1[i][j] * portfolios[i].gammaFactors[j].factorOffered);
        }
        val = val - portfolios[i].planFee - portfolios[i].advisoryFee;
        currentGamma = val;
    }
    return currentGamma;
}

1 个答案:

答案 0 :(得分:0)

for (int j = 0; j < intermediate_gamma_calc1[0].Count; j++)

应该是

for (int j = 0; j < intermediate_gamma_calc1[i].Count; j++)

也许?

----------------------------------------上面不是答案 - ----------------------------------

我想这就是那个:

currentGamma = val;

应该是

currentGamma += val;