C#在一个对象中返回多个值 [英] C# return multiple values in an object

查看:150
本文介绍了C#在一个对象中返回多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法(AlphaCalcResult),应该返回多个值.每个forloop都有一些逻辑,这些逻辑将值分配给List字段.然后,我想返回结果对象,其中包含从下面描述的两个循环生成的两个值. C#开发的新手,语法有问题.

I have a method (AlphaCalcResult) that should return multiple values. Each forloop has some logic which assigns value to a List field. I would then like to return the result object with the two values generated from two loops described below. New to C# development and having issues with the syntax.

我如何在下面的代码中使用return语句做到这一点?

How can I have return statement do that in my code below?

public AlphaCalcResult CalculateAlpha(AlphaCalcParam param)
{
    AlphaCalcResult result = new AlphaCalcResult
    {
        portfolios = new List<Portfolio>()
    };

    // Lists Portfolios & Settings
    var portfolioListItem = param.portfolios.portfolioHoldings;
    var scoreListItem = param.settings.grossAlphas;

    // 1. CALCULATE FUND ALPHA
    for (var i = 0; i<portfolioListItem.Count; i++)
    {
        portfolioListItem[i].fundRating = i;

        for (var j = 0; j<scoreListItem.Count; j++)
        {
            scoreListItem[j].fundRating = j;

            if(i == j)
            {
               portfolioListItem[i].fundAlpha = scoreListItem[j].grossAlpha - portfolioListItem[i].fundExpenseRatio;
            }
        }
    }

    //2. CALCULATE PORTFOLIO ALPHA
    var portfolioAlphaResult = param.portfolios.portfolioAlpha;
    for (var i =0; i<portfolioListItem.Count; i++)
    {
        portfolioAlphaResult = portfolioListItem[i].fundWeight * portfolioListItem[i].fundAlpha;
    };

    // populate result
    return result;
    }

推荐答案

首先将public IEnumerable<Portfolio> portfolios { get; set; }更改为

public List<Portfolio> portfolios { get; set; } 

因为IEnumerable用于读取列表,并在您的代码中添加

because IEnumerable is used for reading the list and in your code add

 //2. CALCULATE PORTFOLIO ALPHA
         for (var i =0; i<portfolioListItem.Count; i++)
            {

        List<PortfolioHolding> list = new List<PortfolioHolding>();
        list=portfolioListItem;
        Portfolio port = new Portfolio();
        port.portfolioIdentifier = "current";
        port.portfolioAlpha = portfolioListItem[i].fundWeight * portfolioListItem[i].fundAlpha;
        port.portfolioHoldings = list;
        result.portfolios.Add(port);

        };

这篇关于C#在一个对象中返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆