我得到下列类别的列表。用逗号连接字符串以加入长字符串
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