gpt4 book ai didi

C#引用参数用法

转载 作者:太空宇宙 更新时间:2023-11-03 19:51:30 26 4
gpt4 key购买 nike

我正在使用引用参数返回多个信息。喜欢,

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com