手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >把jQuery的each(callback)方法移植到c#中
把jQuery的each(callback)方法移植到c#中
摘要:$("img").each(function(i){this.src="test"+i+".jpg";});就可以给给所有图像设置src属性...

$("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#中】相关文章:

无法在Web服务器上启动调试。未将项目配置为进行调试

.net 添加Cookie的4种方法

读写xml所有节点个人小结 和 读取xml节点的数据总结

asp.net图片上传生成缩略图的注意事项

基于ASP.NET的数据迁移方法 dbf上传

asp.net中C++单例实现问题分析

c#中实现文件拖放打开的方法

白刃之战:PHP vs. ASP.NET(节选)-架构比较

asp.net+js实现的ajax sugguest搜索提示效果

ASP.NET使用gridview获取当前行的索引值

精品推荐
分类导航