如何使这更好的code和更优化 [英] how to make this better code and more optimized
本文介绍了如何使这更好的code和更优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我显示从开始的每一个名称的第一个字母,一路到Z
有些事情是这样的:
一个结果
安东尼结果
艾伦结果
...结果
乙结果
鲍勃结果
构建结果
....结果
Ç结果
Charyl结果
卡尔结果
...结果
ž结果
Zoah结果
....结果
我怎样才能让这个code更加优化,并使用更少的线?
INT _a = 0;
INT _b = 0;
INT _c = 0;
...
...
..
INT _z = 0;保护无效列表项(列表<客户> .Enumerator卡斯特)
{如果(cust.MoveNext())
{
顾客T = cust.Current;字符串[]列表= {A,B,C,D,E,F,G,H,I,J,K, 升,M,N,O,p,q,R,S,T,U,v,W,× ,v,Z};
无功输出= list.Aggregate(,(目前,列表项)=>当前+(Environment.NewLine +< H1 ID ='+ listitem.ToUpper()+'><跨度>中+ listitem.ToUpper()+&下; /跨度>&下; / H1>中));
串_name = t.Name.Substring(0,1).ToUpper();
如果(_name ==列表[0] .ToUpper())
{
如果(_a == 0)
{
L =新文字();
l.Text =< H1 ID ='A'><跨度>一种< / SPAN>< / H1>中+ Environment.NewLine;
.....
_a = 1;
}
如果(_name ==名单[1] .ToUpper())
{
如果(_B == 0)
{
L =新文字();
l.Text =下; H1的id ='B'>&下;跨度>一种与所述; /跨度>&下; / H1>中+ Environment.NewLine;
.....
_b = 1;
}
}
...
....
....
...到Z做}
解决方案
我会改变反复code这个想法。
列表<焦炭> HeaderOf =新的List<&烧焦GT;();保护无效列表项(列表<客户> .Enumerator卡斯特)
{
如果(cust.MoveNext())
{
顾客T = cust.Current;
字符串[]列表= {A,B,C,D,E,F,G,H,I,J,K, 升,M,N,O,p,q,R,S,T,U,v,W,× ,v,Z};
无功输出= list.Aggregate(,(目前,列表项)=>当前+(Environment.NewLine +< H1 ID ='+ listitem.ToUpper()+'><跨度>中+ listitem.ToUpper()+&下; /跨度>&下; / H1>中)); 炭CheckMe = t.Name.Substring(0,1).ToUpper()[0];
如果(!HeaderOf.Contains(CheckMe))
{
HeaderOf.Add(CheckMe); L =新文字();
l.Text =< H1 ID ='+ CheckMe +'><跨度>中+ CheckMe +&下; /跨度>&下; / H1>中+ Environment.NewLine;
}
}
}
so i am displaying first letter of the each Name starting from A and all the way to Z
some thing like this:
A
Anthony
Allan
...
B
Bob
Builder
....
C
Charyl
Carl
...
Z
Zoah
....
how can i make this code more optimized and use less line?
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", "v", "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
}
解决方案
I will change the repeated code with this idea.
List<char> HeaderOf = new List<char>();
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", "v", "z" };
var output = list.Aggregate("", (current, 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;
}
}
}
这篇关于如何使这更好的code和更优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文