1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using static System.Math;
 5 
 6 namespace ConsoleApp1
 7 {
 8     public class CSharp6
 9     {
10         //属性默认值
11         public static string Name { get; set; } = "jefft";        
12         public IList<int> AgeList
13         {
14             get;
15             set;
16         } = new List<int> { 10, 20, 30, 40, 50 };
17 
18         //Dic新的赋值方法
19         public IDictionary<int, string> DicNew = new Dictionary<int, string>()
20         {
21             [4] = "first",
22             [5] = "second"
23         };
24 
25         //属性Lambda表达式
26         public static string NameField => string.Format("姓名: {0}", Name);
27         public static void PrintNameField() => Console.WriteLine(NameField);
28 
29         public static void TestStaticImport()
30         {          
31             Console.WriteLine($"导入Math静态类之后可直接使用方法: Pow(4, 2) = {Pow(4, 2)}");             
32         }
33 
34         public static void TestNullKey()
35         {
36             int? x = null;
37             int? y = x + 40;
38             int? z = 40 + x ;
39 
40             Console.WriteLine($"?.符号的使用: X={x},Y={y?.ToString()}, Z={z?.ToString()}");
41         }
42          
43         public static void TestExceptionFilter(int exceptionValue = 10)
44         {        
45             try
46             {
47                 Int32.Parse("s");
48             }
49             catch (Exception e) when (exceptionValue > 1)//满足条件才进入catch
50             {
51                 Console.WriteLine($"条件满足下捕捉:{e.Message}");               
52             } 
53             catch (Exception e)
54             {
55                 Console.WriteLine(e.Message);
56             }
57         }
58 
59         public static void TestNameOf()
60         {
61              Console.WriteLine("nameof测试结果");
62              Console.WriteLine(nameof(Name));
63              Console.WriteLine(nameof(NameField));
64              Console.WriteLine(nameof(PrintNameField));
65         }
66     }     
67 }
 1  static void Csharp6Test()
 2         {
 3             CSharp6.PrintNameField();
 4             CSharp6.TestStaticImport();
 5             CSharp6.TestNullKey();
 6             CSharp6.TestExceptionFilter();
 7             CSharp6.TestNameOf();
 8 
 9             Console.ReadLine();
10         }

 

posted on 2019-08-21 14:33  极简  阅读(172)  评论(0编辑  收藏  举报