diff --git "a/42\351\231\210\344\275\263\344\274\246/20230522\344\275\234\344\270\232.md" "b/42\351\231\210\344\275\263\344\274\246/20230522\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..b711d0a3fa526c9a494282908753645880b7aaf1 --- /dev/null +++ "b/42\351\231\210\344\275\263\344\274\246/20230522\344\275\234\344\270\232.md" @@ -0,0 +1,64 @@ + +```c# +/// +/// 筛选偶数,并从小到大排序 +/// + +var arr=new int[]{1,4,5,10,5}; + +//linq表达式 + +var arr1=from s in arr where s % 2==0 orderby s ascending select s; + +//链式写法 + +var arr2=arr.Where(x=>x%2==0).OrderBy(x=>x); + +//遍历输出 +foreach (var item in arr1) +{ + Console.WriteLine(item); +} + +foreach (var item in arr2) +{ + Console.WriteLine(item); +} +``` +![show](https://gitee.com/Cchen-jialun/picture/raw/master/20230524/zIXBw4acbAGL.png) + + +```c# +/// +/// 统计随机数出现的次数。 +/// + +var random = new Random(); +var arr = new int[100]; + +for (int i = 0; i < arr.Length; i++) +{ + arr[i] = random.Next(100); +} +//定义字典存储键以及值 +var dictionary = new Dictionary(); + +//遍历存储键值 +foreach (var a in arr) +{ + if (!dictionary.ContainsKey(a)) + { + dictionary[a] = 1; + } + else + { + dictionary[a]++; + } +} +//遍历输出 +foreach (var i in dictionary) +{ + Console.WriteLine(i); +} +``` +![show](https://gitee.com/Cchen-jialun/picture/raw/master/20230524/JM5RmkZKTpQK.png) \ No newline at end of file diff --git "a/42\351\231\210\344\275\263\344\274\246/20230523\344\275\234\344\270\232.md" "b/42\351\231\210\344\275\263\344\274\246/20230523\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..1d5fbb791cc74af4593949bc17853d0b22372982 --- /dev/null +++ "b/42\351\231\210\344\275\263\344\274\246/20230523\344\275\234\344\270\232.md" @@ -0,0 +1,53 @@ +```c# +/// +/// 统计每个字符出现的次数 +/// + +var strings = new String[] { "apple", "orange", "banner" }; + +//linq表达式写法 +var array = from s in strings from i in s group i by i into g orderby g.Count() descending select new { g.Key, count = g.Count() }; + +//链式写法 +var array2 = strings.SelectMany(s => s).GroupBy(x => x).OrderByDescending(g => g.Count()).Select(g => new { g.Key, count = g.Count() }); + +//遍历输出 +foreach (var i in array) +{ + Console.WriteLine($"{i.Key}:{i.count}"); +} + +Console.WriteLine("===================================================================================="); + +foreach (var i in array2) +{ + Console.WriteLine($"{i.Key}:{i.count}"); +} +``` +![show](https://gitee.com/Cchen-jialun/picture/raw/master/20230524/!v@ok6m5P4wt.png) + +```c# +/// +/// 使用AsParallel模拟同步下载三张图片 +//结果: +/* + pic1 下载完成 + pic3 下载完成 + pic2 下载完成 +*/ +/// + +string pic1 = "4b069d702e7fec0dc06cf5815ece8503.jpeg"; +string pic2 = "4b069d702e7fec0dc06cf5815esn7005.jpeg"; +string pic3 = "4b069d702e7fec0dc06cf5815ets6730.jpeg"; + +var pictures = new String[] { pic1, pic2, pic3 }; + + +pictures.AsParallel().ForAll(x => +{ + Thread.Sleep(2000); + Console.WriteLine($"{x}下载完成"); +}); +``` +![show](https://gitee.com/Cchen-jialun/picture/raw/master/20230524/fvFO7*0z5Mhh.png) \ No newline at end of file