C#基础学习

连接多个字符串  

 1 string[] words = { "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog." };
 2 
 3 var unreadablePhrase = string.Concat(words);
 4 System.Console.WriteLine(unreadablePhrase);
 5 
 6 
 7 var readablePhrase = string.Join(" ", words);
 8 System.Console.WriteLine(readablePhrase);
 9 
10 输出:
11 Thequickbrownfoxjumpsoverthelazydog.
12 The quick brown fox jumps over the lazy dog.

 

 1 string[] fruits = { "grape", "passionfruit", "banana", "abcdef", "mango",
 2                       "orange", "raspberry", "apple", "blueberry" };
 3 
 4 var result1 = fruits.OrderBy(fruit => fruit).ThenBy(fruit => fruit.Length);
 5 
 6 Console.WriteLine(string.Join(",", result1));
 7 输出:
 8 abcdef,apple,banana,blueberry,grape,mango,orange,passionfruit,raspberry
 9 
10 Console.WriteLine(string.Concat(result1.Select(a=>a+"\r\n")));
11 输出:
12 abcdef
13 apple
14 banana
15 blueberry
16 grape
17 mango
18 orange
19 passionfruit
20 raspberry

OrderBy:按升序对序列的元素进行排序。

OrderByDescending:使用指定的比较器按降序对序列的元素排序。

 ThenBy:按升序对序列中的元素执行后续排序。

ThenByDescending:按降序对序列中的元素执行后续排序。

https://docs.microsoft.com/zh-cn/dotnet/api/system.linq.enumerable?view=net-6.0

合并数组

1  Console.WriteLine(string.Join("-",words.Concat(fruits)));
  // 使用 words.Union(fruits) 会剔除重复数据项, Concat 会保留重复数据项
输出:
The-quick-brown-fox-jumps-over-the-lazy-dog.-grape-passionfruit-banana-abcdef-mango-orange-raspberry-apple-blueberry

 对集合排序,并输出

var alist = new Dictionary<string,string>();
alist.Add("001", "asdf");
alist.Add("004", "gewqe");
alist.Add("009", "23fesf");
alist.Add("002", "refa");
alist.Add("005", "ewre");
Console.WriteLine(string.Join(",",alist.OrderBy(a => a.Key).ThenBy(b => b.Value)));

输出:
[001, asdf],[002, refa],[004, gewqe],[005, ewre],[009, 23fesf]

 

https://docs.microsoft.com/zh-cn/dotnet/csharp/tutorials

原文地址:https://www.cnblogs.com/shouwu/p/15637972.html