C# 敏捷1

2021-06-27 01:07

阅读:582

标签:col   tle   created   lam   post   eric   ide   null   cores   

  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 Comments { get; private set; } = new List();
 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 Dictionarystring, string>
 41               {
 42                    { "key1","value1"},
 43                     { "key2","value2"}
 44                };
 45 
 46             //新特性
 47             var dictionary1 = new Dictionarystring, 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             Liststring, string, string>> result = new Liststring, string, string>>();
 68             result.Add(new Tuplestring, 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 Tupleint, int>(1, 2);  // 使用 new 运算符创建元组
 88             Tupleint, 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             Tuplestring, Nullableint>>[] scores =
 94                     { new Tuplestring, Nullableint>>("Jack", 78),
 95                       new Tuplestring, Nullableint>>("Abbey", 92),
 96                       new Tuplestring, Nullableint>>("Dave", 88),
 97                       new Tuplestring, Nullableint>>("Sam", 91),
 98                       new Tuplestring, Nullableint>>("Ed", null),
 99                       new Tuplestring, Nullableint>>("Penelope", 82),
100                       new Tuplestring, Nullableint>>("Linda", 99),
101                       new Tuplestring, Nullableint>>("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 Tupleint, int> Divide(int dividend, int divisor)
109         {
110             int result = dividend / divisor;
111             int reminder = dividend % divisor;
112             return Tuple.Createint, int>(result, reminder);           //返回两个相同类型元素的元组
113         }
114         public static Tupleint, string> M()
115         {
116             Tupleint, string> tuple = null;
117             return tuple = new Tupleint, string>(0, "Sucess");
118         }
119 
120 
121         private static double ComputeMean(Tuplestring, Nullableint>>[] 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 }

 

C# 敏捷1

标签:col   tle   created   lam   post   eric   ide   null   cores   

原文地址:https://www.cnblogs.com/LiuFengH/p/10088524.html


评论


亲,登录后才可以留言!