加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何使这个更好的代码和更优化

发布时间:2020-12-16 10:00:30 所属栏目:asp.Net 来源:网络整理
导读:所以我显示每个名字的第一个字母,从A开始,一直到Z. 这样的事情: 一个 安东尼 艾伦 … 乙 短发 生成器 …. C Charyl 卡尔 … ?? Zoah …. 如何使这些代码更优化并使用更少的线? int _a = 0;int _b = 0;int _c = 0;........int _z = 0;protected void ListIt
所以我显示每个名字的第一个字母,从A开始,一直到Z.

这样的事情:

一个
安东尼
艾伦


短发
生成器
….

C
Charyl
卡尔

??
Zoah
….

如何使这些代码更优化并使用更少的线?

int _a = 0;
int _b = 0;
int _c = 0;
...
...
..
int _z = 0;

protected void ListItem(List<Customer>.Enumerator cust)
{

if (cust.MoveNext())
 {
    Customer t = cust.Current;

string[] list = { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","z" };
var output = list.Aggregate("",(current,listitem) => current + (Environment.NewLine + "<h1 id='" + listitem.ToUpper() + "'><span>" + listitem.ToUpper() + "</span></h1>"));
string _name = t.Name.Substring(0,1).ToUpper();


if (_name == list[0].ToUpper())
{
   if (_a == 0)
   {
      l = new Literal();
      l.Text = "<h1 id='A'><span>A</span></h1>" + Environment.NewLine;
      .....
      _a = 1;
  }
if (_name == list[1].ToUpper())
{
  if (_b == 0)
  {
    l = new Literal();
    l.Text = "<h1 id='B'><span>A</span></h1>" + Environment.NewLine;
    .....
    _b = 1;
  }
}
...
....
....
...

doing through Z

}

解决方法

我会用这个想法改变重复的代码.

List<char> HeaderOf = new List<char>();

protected void ListItem(List<Customer>.Enumerator cust)
{
    if (cust.MoveNext())
    {
        Customer t = cust.Current;
        string[] list = { "a","z" };
        var output = list.Aggregate("",listitem) => current + (Environment.NewLine + "<h1 id='" + listitem.ToUpper() + "'><span>" + listitem.ToUpper() + "</span></h1>"));

        char CheckMe = t.Name.Substring(0,1).ToUpper()[0];
        if (!HeaderOf.Contains(CheckMe))
        {
            HeaderOf.Add(CheckMe);

            l = new Literal();
            l.Text = "<h1 id='" + CheckMe + "'><span>" + CheckMe + "</span></h1>" + Environment.NewLine;
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读