C#引用参数用法

标签 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<string, int> totalInfos = new Dictionary<string, int>
{
    { "totalTransaction", 0 },
    { "outTransaction", 0 },
    { "totalRecord", 0 }
};

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

但还是不够好,因为没有 promise 关键字符串,就像硬编码。

我需要使用 Constant 作为键吗?或者针对这种情况有更好的解决方案吗?

最佳答案

只需使用一个类。并完全避免 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 = ...
    };
}

关于C#引用参数用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38675383/

相关文章:

c# - 如何引用我的 .net 程序的安装目录?

c# - UWP Windows 10 未触发 OnSuspending 事件

c# - MassTransit 在 Azure 服务总线上创建队列

c# - 如何使用数据库中的值设置标签的文本?

c# - 检查元素是否存在或为空

c# - 使用 Linq 从列表中获取所有匹配值的索引

C# 接口(interface)和基类

c# - 在 C# 中调用由参数确定的方法

c# - sql server 从表中选择百分比并从另一个表中选择所有数据

c# - 将 B2C 回复 URL 从 "signin-oidc"更改为其他内容不起作用