国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2020-01-18 01:37:06
字體:
來源:轉載
供稿:網友

$("img").each(function(i){ 
this.src = "test" + i + ".jpg"; 
});  


就可以給給所有圖像設置src屬性。

c#中雖然有for(;;)和foreach(..in )可以完成此功能,

        static void Main(string[] args) 
        { 
            string[] arr = new string[] { "A", "B", "C", "D", "E" }; 
            foreach (string item in arr) 
            { 
                Console.WriteLine(item); 
            } 
            Console.ReadKey(); 
        } 


但和jQuery的each(callback)比起來還顯得復雜了點。

現在使用c#3.0的擴展方法功能來將each(callback)移植到c#中來。然后我們就可以用這段代碼替換上面的了。


        static void Main(string[] args) 
        { 
            string[] arr = new string[] { "A", "B", "C", "D", "E" }; 
            arr.Each(p => Console.WriteLine(p)); 
            Console.ReadKey(); 
        } 



比foreach簡便多了吧,實現代碼就幾行。

    public delegate void EachDelegate<T>(T arg); 
    public static class IEnumerableExtension 
    { 
        public static void Each<T>(this IEnumerable<T> src, EachDelegate<T> callback) 
        { 
            foreach (T item in src) 
            { 
                callback(item); 
            } 
        } 
    } 
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 江源县| 靖州| 汶上县| 宽城| 威海市| 金平| 宁陕县| 温宿县| 昌宁县| 西贡区| 南皮县| 东城区| 承德县| 宁强县| 建宁县| 新疆| 乌恰县| 五莲县| 正宁县| 姜堰市| 德阳市| 桐乡市| 桃江县| 舟曲县| 巴彦淖尔市| 张家川| 灵台县| 德惠市| 黔西| 合水县| 岳阳市| 平顶山市| 梧州市| 龙海市| 和田市| 丰城市| 晋江市| 东海县| 高阳县| 长白| 广宁县|