1

我正在获得以下课程的列表。

 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;
            }
        }
    }

代码按预期工作,但可以以任何方式优化/改进。我觉得范围有所改善,但无法弄清楚??

4

4 回答 4

4

StringBuilder在 foreach 循环中附加字符串会更有效(取决于迭代次数)

private void PrepareStatusString(List<SmsResponse> responseList)
{
    bool isfirst = true;
    StringBulder sb = new StringBuilder();
    foreach (var item in responseList)
    {
        if (item.Response != 0)
        {
            if(isfirst)
                sb.AppendFormat("{0}|{1}|{2}", item.AliasName, item.CellPhoneNumber,item.Response.ToString());
            else
                sb.AppendFormat(",{0}|{1}|{2}", item.AliasName, item.CellPhoneNumber, item.Response.ToString());

            isfirst = false;
        }
    }

    StatusDescription = sb.ToString();
}
于 2013-07-05T10:17:36.663 回答
4

如果您使用的是 .NET 4 或更新版本,您可以覆盖SmsResponse.ToString()然后使用String.Join<T>(String, IEnumerable<T>)来连接响应。

所以你的SmsResponse班级可能看起来像这样:

public class SmsResponse
{
    public string AliasName { get; set; }
    public string CellPhoneNumber { get; set; }
    public int Response { get; set; }

    public override string ToString()
    {
        return AliasName + "|" + CellPhoneNumber + "|" +
            Response.ToString();
    }
}

并且PrepareStatusString会是:

private void PrepareStatusString(List<SmsResponse> responseList)
{
    StatusDescription = string.Join(",", responseList.Where(i => i.Response != 0));
}
于 2013-07-05T10:23:28.243 回答
1

我不知道优化,但它可以更富有表现力地重写如下:

private void PrepareStatusString(List<SmsResponse> responseList)
{
    StatusDescription = responseList
         .Where(x => x.Response != 0)
         .Select(x => x.AliasName 
                    + "|" + x.CellPhoneNumber 
                    + "|" + x.Response.ToString())
         .Aggregate((x, y) => x + "," + y);
}

请注意,StringBuilder如果您预计那里有几百个以上的对象,这只会提供明显的性能优势。

于 2013-07-05T10:20:26.707 回答
0

string.Join像这样使用

    List<string> elements = new List<string>();
    foreach (var item in responseList)
    {
        if (item.Response != 0)
        {
            elements.add(item.AliasName + "|" + item.CellPhoneNumber + "|" + item.Response.ToString());
        }
    }
    string result = string.Join(",", elements.ToArray());
于 2013-07-05T10:18:11.613 回答