我得到下列類別的列表。用逗號連接字符串以加入長字符串
public class SmsResponse
{
public string AliasName { get; set; }
public string CellPhoneNumber { get; set; }
public int Response { get; set; }
}
我通過這個列表的功能,以檢查是否響應字段比0的響應等,如果有比它爲我所用這種方法PrepareStatusString();
準備狀態字符串錯誤。
bool isSuccess = EvaluateSmsResponse(responseList); //list of smsresponse class
private bool EvaluateSmsResponse(List<SmsResponse> smsResponseList)
{
bool isSent = smsResponseList.Exists(response => response.Response != 0);
if (!isSent)
PrepareStatusString(smsResponseList);
return isSent;
}
private void PrepareStatusString(List<SmsResponse> responseList)
{
bool isfirst = true;
foreach (var item in responseList)
{
if (item.Response != 0)
{
if(isfirst)
StatusDescription += item.AliasName + "|" + item.CellPhoneNumber + "|" + item.Response.ToString();
else
StatusDescription += "," + item.AliasName + "|" + item.CellPhoneNumber + "|" + item.Response.ToString();
isfirst = false;
}
}
}
該代碼工作正常,但可以通過任何方式進行優化/改進。我感覺有一個範圍改善,但無法弄清楚?
我正在使用.NET框架4.0,也喜歡的解決方案,但它給了我的函數的字符串關鍵字公開覆蓋字符串ToString()聲明「期望的類,委託,枚舉,接口或結構」錯誤 – ankur
@ankur看起來你已經將'ToString()'方法複製到命名空間範圍內,而不是類範圍。 –
是超出了課程範圍。它工作完全乾淨和簡單.... – ankur