C# 将聚合反向工程为简单的for循环
有人能帮我把这段代码改回简单的for循环吗C# 将聚合反向工程为简单的for循环,c#,C#,有人能帮我把这段代码改回简单的for循环吗 public class sT { public string[] Description { get; set; } } text = sT.Description.Aggregate(text, (current, description) => current + ("<option value=" + string.Format("{0:00}", j) + "'>" + j++ + ". "
public class sT
{
public string[] Description { get; set; }
}
text = sT.Description.Aggregate(text, (current, description) =>
current + ("<option value=" + string.Format("{0:00}", j) + "'>" + j++ + ". " + description+ "</option>"));
公共类sT
{
公共字符串[]说明{get;set;}
}
text=sT.Description.Aggregate(文本,(当前,描述)=>
current+(“Aggregate”只是循环遍历列表中的项目,并将委托的结果传递给委托的下一个调用
第一个参数指定要开始的初始值
foreach ( string description in sT.Description )
{
text += "<option value=" + string.Format("{0:00}", j) + "'>" + j++ + ". " + description+ "</option>";
}
foreach(sT.description中的字符串描述)
{
text+=“看起来像这样
foreach (string s in sT.Description)
text = text + ("<option value=" + string.Format("{0:00}", j) + "'>" + j++ + ". " + s + "</option>");
foreach(sT.Description中的字符串s)
text=text+(“foreach(sT.description中的变量描述)
{
text+=String.Format(“您可以使用
.Aggregate(new StringBuilder(),
(a, b) => a.Append(", " + b.ToString()),
(a) => a.Remove(0, 2).ToString());
或
公共静态字符串聚合(此IEnumerable源代码,Func fn)
{
StringBuilder sb=新的StringBuilder();
foreach(源中的字符串s)
{
如果(某人长度>0)
某人加上(“,”);
某人追加;
}
使某人返回字符串();
}
我可以建议您吗
foreach (string s in sT.Description)
text += string.Format("<option value='{0:00}'>{1}. {2}</option>", j, j++, s);
foreach(sT.Description中的字符串s)
text+=string.Format(“{1}.{2}”,j,j++,s);
或者,更确切地说:
text += sT.Description.Aggregate(new StringBuilder(),
(a,s) => a.AppendFormat("<option value='{0:00}'>{1}. {2}</option>", j, j++, s)
).ToString();
text+=sT.Description.Aggregate(新的StringBuilder(),
(a,s)=>a.AppendFormat(“{1}.{2}”,j,j++,s)
).ToString();
我认为它更清晰,当然也更有效。但是,如果您坚持为此创建一个循环,您可以使用:
var sb = new StringBuilder();
foreach (string s in sT.Description)
sb.AppendFormat("<option value='{0:00}'>{1}. {2}</option>", j, j++, s);
text += sb.ToString();
var sb=new StringBuilder();
foreach(sT.Description中的字符串s)
sb.AppendFormat(“{1}.{2}”,j,j++,s);
text+=sb.ToString();
你的意思是,重写,而不是逆向工程。另外,经典的例子是:为什么不告诉我们你需要做什么不同的处理?属性的值可以忽略开头的引号,但它与问题无关。这看起来像是字符串的一个非常糟糕的复制。对我来说,Join
。还有,你呢在现实生活中像这样缩进你的代码?答案的技术准确性和正确性不是由我来决定的,但是,以后请不要再把代码样本放在一堆乱七八糟的东西里。下次它会被删除,或者你可能会发现自己因为低质量的贡献而被暂停。你可以了解更多关于如何使用m这里是arkdown编辑器:
text += sT.Description.Aggregate(new StringBuilder(),
(a,s) => a.AppendFormat("<option value='{0:00}'>{1}. {2}</option>", j, j++, s)
).ToString();
var sb = new StringBuilder();
foreach (string s in sT.Description)
sb.AppendFormat("<option value='{0:00}'>{1}. {2}</option>", j, j++, s);
text += sb.ToString();