2016-07-30 3 views
1

여러 정보를 반환하기 위해 참조 매개 변수를 사용하고 있습니다. 같은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); 

하지만 키 스트링이 약속되어 있지 않기 때문에 아직 충분히 좋지 않습니다. 하드 코딩과 같습니다.

키에 Constant를 사용해야합니까? 또는 그 경우에 대한 더 나은 해결책은 무엇입니까?

+3

속성을 사용하여 해당 정보를 모두 표시하는 클래스를 만들면 안됩니까? –

+1

이러한 경고의 전부는 아니지만 다음과 같이 동의합니다. https://msdn.microsoft.com/en-us/library/ms182131.aspx 'out'매개 변수가 필요한 이유를 정말로 이해하지 못한다면 그들. – starlight54

답변

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 = ... 
    }; 
} 
+0

정말 고마워요! –