C#参考参数用法

时间:2016-07-30 15:08:40

标签: c#

我使用引用参数返回多个信息。等,

int totalTransaction = 0;
int outTransaction = 0;
int totalRecord = 0;

var record = reports.GetTransactionReport(searchModel, out totalTransaction, out outTransaction, out totalRecord);

//并且方法是这样的,

public List<TransactionReportModel> GetAllTransaction(
            TransactionSearchModel searchModel, 
            out totalTransaction,
            out totalTransaction,
            out totalRecord) {


    IQueryable<TransactionReportModel> result;
    // search

    return result.ToList();
}

但我不喜欢长参数,所以我试图用单个参数来清理它,使用Dictionary。

Dictionary<string, int> totalInfos = new Dictionary<string, int>
{
    { "totalTransaction", 0 },
    { "outTransaction", 0 },
    { "totalRecord", 0 }
};

var record = reports.GetTransactionReport(searchModel, out totalInfos);

但仍然不够好,因为关键字符串没有得到承诺,它就像硬编码一样。

我需要使用Constant作为键吗?还是针对那种情况的任何更好的解决方案?

1 个答案:

答案 0 :(得分:5)

只需使用一堂课。并完全避免out个参数:

class TransactionResult
{
    public List<TransactionReportModel> Items { get; set; }

    public int TotalTransaction { get; set; }
    public int OutTransaction { get; set; }
    public int TotalRecord { get; set; }
}


public TransactionResult GetAllTransaction(TransactionSearchModel searchModel)
{
    IQueryable<TransactionReportModel> result;
    // search

    return new TransactionResult
    { 
        Items = result.ToList(),
        TotalTransaction = ...,
        OutTransaction = ...,
        TotalRecord = ...
    };
}