zoukankan      html  css  js  c++  java
  • C# 6.0 新特征 Demo

     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         }

  • 相关阅读:
    迭代器和生成器
    装饰器
    函数进阶二
    函数进阶
    函数的初识
    python基础七
    python基础六
    python基础五
    python基础四
    python2与python3的区别
  • 原文地址:https://www.cnblogs.com/AspDotNetMVC/p/11388590.html
Copyright © 2011-2022 走看看