1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Diagnostics; 5 using System.Linq; 6 using System.Text; 7 using System.Threading.Tasks; 8 9 namespace ConsoleApplication2 10 { 11 public class Post 12 { 13 public string Title { get; set; } = "Title"; 14 public string Content { get; set; }= "Content"; 15 16 public int? age { get; set; } = 0; 17 //新特性 18 public DateTime DateCreated { get; private set; } = DateTime.Now; 19 public List<Comment> Comments { get; private set; } = new List<Comment>(); 20 public int AddOld() 21 { 22 return 1 + 1; 23 } 24 25 //新特性还是用Lambda的语法而已 26 public int AddNew() => 1+1; 27 public int AddNew(int a) => 1 + a; 28 29 } 30 31 public class Comment 32 { 33 34 } 35 class Program 36 { 37 38 static void Main(string[] args) 39 { 40 var dictionary = new Dictionary<string, string> 41 { 42 { "key1","value1"}, 43 { "key2","value2"} 44 }; 45 46 //新特性 47 var dictionary1 = new Dictionary<string, string> 48 { 49 ["key1"] = "value1", 50 ["key2"] = "value2" 51 }; 52 Console.WriteLine(dictionary); 53 54 Post post = new Post(); 55 56 string t1 = string.Format("{0}_{1}", post.Title, post.Content); 57 //新特性 58 string t2 = $"{post.Title}_{post.Content}"; 59 Console.Write(t1); Console.Write("-"); Console.WriteLine(t2); 60 61 Console.Write(post.AddOld()); Console.WriteLine(post.AddNew(2)); 62 //if (x in (1, 2, 3)) 63 //if (x in 1:5) 64 //if (x between(1, 5)) 65 66 67 List<Tuple<string, string, string>> result = new List<Tuple<string, string, string>>(); 68 result.Add(new Tuple<string, string, string>("fa", "q:" , "")); 69 70 string obj = null ; 71 72 switch (obj) 73 { 74 case "+" : 75 Console.WriteLine(1); 76 break; 77 case null: 78 Console.WriteLine(2); 79 break; 80 81 default: 82 Console.WriteLine(3); 83 break; 84 } 85 //var tuple = (1, 2); // 使用语法糖创建元组 86 var tuple2 = Tuple.Create(1, 2); // 使用静态方法【Create】创建元组 87 var tuple3 = new Tuple<int, int>(1, 2); // 使用 new 运算符创建元组 88 Tuple<int, int> r = Divide(13, 2); 89 Console.WriteLine($"{r.Item1}_{r.Item2}"); 90 var z = M(); 91 Console.WriteLine($"{z.Item1}_{z.Item2}"); 92 93 Tuple<string, Nullable<int>>[] scores = 94 { new Tuple<string, Nullable<int>>("Jack", 78), 95 new Tuple<string, Nullable<int>>("Abbey", 92), 96 new Tuple<string, Nullable<int>>("Dave", 88), 97 new Tuple<string, Nullable<int>>("Sam", 91), 98 new Tuple<string, Nullable<int>>("Ed", null), 99 new Tuple<string, Nullable<int>>("Penelope", 82), 100 new Tuple<string, Nullable<int>>("Linda", 99), 101 new Tuple<string, Nullable<int>>("Judith", 84) }; 102 int number; 103 double mean = ComputeMean(scores, out number); 104 Console.WriteLine("Average test score: {0:N2} (n={1})", mean, number); 105 106 Console.ReadKey(true); 107 } 108 public static Tuple<int, int> Divide(int dividend, int divisor) 109 { 110 int result = dividend / divisor; 111 int reminder = dividend % divisor; 112 return Tuple.Create<int, int>(result, reminder); //返回两个相同类型元素的元组 113 } 114 public static Tuple<int, string> M() 115 { 116 Tuple<int, string> tuple = null; 117 return tuple = new Tuple<int, string>(0, "Sucess"); 118 } 119 120 121 private static double ComputeMean(Tuple<string, Nullable<int>>[] scores, out int n) 122 { 123 n = 0; 124 int sum = 0; 125 foreach (var score in scores) 126 { 127 if (score.Item2.HasValue) 128 { 129 n += 1; 130 sum += score.Item2.Value; 131 } 132 } 133 if (n > 0) 134 return sum / (double)n; 135 else 136 return 0; 137 } 138 139 } 140 141 142 143 }