把jQuery的each(callback)方法移植到c#中

待到芦花摇曳时,再倚西窗闻雁鸣,日落西山江上行,思缱绻,清月冷,风也寄情。光影斑驳,时光掠过,心于光阴的门楣,寂静欢喜。一纸素文,一缕清风,写意了心中的世界,终有一人的身影站立在风中于梦中,风去,影渐消。水潺潺,风声声,心低喃细语。 "

$("img").each(function(i){
this.src="test"+i+".jpg";
});
就可以给给所有图像设置src属性。 c#中虽然有for(;;)和foreach(..in)可以完成此功能, staticvoidMain(string[]args)
{
string[]arr=newstring[]{"A","B","C","D","E"};
foreach(stringiteminarr)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
但和jQuery的each(callback)比起来还显得复杂了点。 现在使用c#3.0的扩展方法功能来将each(callback)移植到c#中来。然后我们就可以用这段代码替换上面的了。
staticvoidMain(string[]args)
{
string[]arr=newstring[]{"A","B","C","D","E"};
arr.Each(p=>Console.WriteLine(p));
Console.ReadKey();
} 比foreach简便多了吧,实现代码就几行。 publicdelegatevoidEachDelegate<T>(Targ);
publicstaticclassIEnumerableExtension
{
publicstaticvoidEach<T>(thisIEnumerable<T>src,EachDelegate<T>callback)
{
foreach(Titeminsrc)
{
callback(item);
}
}
}

到此这篇关于把jQuery的each(callback)方法移植到c#中就介绍到这了。没有口水与汗水,就没有成功的泪水。更多相关把jQuery的each(callback)方法移植到c#中内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
jQuery+Asp.Net实现省市二级联动功能的方法

详解JQuery Ajax 在asp.net中使用总结

jQuery实现金额录入框

jQuery实现倒计时跳转的例子

浅谈对Jquery+JSON+WebService的使用小结