C#参考参数用法

C#参考参数用法,c#,C#,我使用引用参数返回多个信息。像 int totalTransaction = 0; int outTransaction = 0; int totalRecord = 0; var record = reports.GetTransactionReport(searchModel, out totalTransaction, out outTransaction, out totalRecord); //方法是这样的, public List<TransactionReportMode

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

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();
}
公共列表GetAllTransaction(
TransactionSearchModel搜索模型,
我们的交易,
我们的交易,
(未记录){
可预测的结果;
//搜寻
返回result.ToList();
}
但是我不喜欢长参数,所以我尝试用一个参数来清理它,使用字典

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

var record = reports.GetTransactionReport(searchModel, out totalInfos);
Dictionary totalInfos=新字典
{
{“totalTransaction”,0},
{“outTransaction”,0},
{“totalRecord”,0}
};
var record=reports.GetTransactionReport(searchModel,out totalInfos);
但仍然不够好,因为没有承诺使用关键字符串,这就像硬录制一样


我需要为键使用常量吗?或者有更好的解决方案吗?

只需使用一个类。并完全避免
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 = ...
    };
}
类事务处理结果
{
公共列表项{get;set;}
公共整数TotalTransaction{get;set;}
public int out事务{get;set;}
公共整数TotalRecord{get;set;}
}
public TransactionResult GetAllTransaction(TransactionSearchModel搜索模型)
{
可预测的结果;
//搜寻
返回新的TransactionResult
{ 
Items=result.ToList(),
TotalTransaction=。。。,
OutTransaction=。。。,
TotalRecord=。。。
};
}

只需使用一个类。并完全避免
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 = ...
    };
}
类事务处理结果
{
公共列表项{get;set;}
公共整数TotalTransaction{get;set;}
public int out事务{get;set;}
公共整数TotalRecord{get;set;}
}
public TransactionResult GetAllTransaction(TransactionSearchModel搜索模型)
{
可预测的结果;
//搜寻
返回新的TransactionResult
{ 
Items=result.ToList(),
TotalTransaction=。。。,
OutTransaction=。。。,
TotalRecord=。。。
};
}

为什么不创建一个使用属性公开所有信息的类呢?尽管并非所有这些警告都重要,我同意这一点:除非您真正理解为什么需要一个
out
参数,否则我将避免使用它们。为什么不创建一个类,使用属性公开所有这些信息?尽管并非所有这些警告都很重要,我同意这一点:除非你真的理解为什么需要一个
out
参数,否则我会避免使用它们。非常感谢!非常感谢你!