• 深度解析.NetFrameWork/CLR/C# 以及C#6/C#7新语法


    一:什么是.NetFrameWork/ CLR / C#

    1:.NetFramework即架构,它是一个语言开发软件,提供了软件开发的框架,使开发更具工程性、简便性和稳定性,这个框架主要是针对于c#语言的,该框架包含了CLR,VS等编译工具,BCL(基本类库)。

    2:c#是一个简单的、现代的、通用的、面向对象的编程语言,它是由微软(Microsoft)开发的,主要是为.netFramwork框架提供一种编程规范,即是符合CLR中的CLS(通用语言规范)。

    3:CLR是Common Language RunTime,公共运行类库。c#语言通过编译会生成IL+Metadata两部分,而CL主要的功能是通过JIT即时编译器把这部分解析成机器识别的代码,即二进制编码,然后再计算机中执行。

    二:c# Project运行的整个流程

    执行流程:

    无论是VB或者C#项目都会依赖于BCL(基础类库),然后通过编译生成IL(中间语言)+Metadata(列表清单),然后通过JIT(Just IN Time )编译成机器二进制码(因为计算机只识别二进制码),然后再在计算机中执行。

    三:.NetFrameWork/ CLR / C#对应的版本

    注意:并不是每个版本都一一对应的,有的版本升级然后CLR并没有升级,visualStudio是.net开发工具。

    下图也能说明vs与c#版本:

    四:c#既然是.netFramework的规范,那下面介绍一下c#6以及c#7新的语法

    1:c#6的新语法

    如图:

    具体代码如下:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Text;
      4 using System.Threading;
      5 using System.Threading.Tasks;
      6 using static System.Math;
      7 
      8 namespace Test.Project
      9 {
     10     /// <summary>
     11     /// C#6 新语法
     12     /// </summary>
     13     public class SharpSix
     14     {
     15         #region 自动属性初始化(Auto-property initializers)
     16         public string Name { get; set; } = "summit";
     17         public int Age { get; set; } = 22;
     18         public DateTime BirthDay { get; set; } = DateTime.Now.AddYears(-20);
     19         public IList<int> AgeList
     20         {
     21             get;
     22             set;
     23         } = new List<int> { 10, 20, 30, 40, 50 };
     24         #endregion
     25 
     26         public void Show(People peopleTest)
     27         {
     28             #region 字符串嵌入值(String interpolation)
     29             Console.WriteLine($"年龄:{this.Age}  生日:{this.BirthDay.ToString("yyyy-MM-dd")}");
     30             Console.WriteLine($"年龄:{{{this.Age}}}  生日:{{{this.BirthDay.ToString("yyyy -MM-dd")}}}");
     31 
     32             Console.WriteLine($"{(this.Age <= 22 ? "小鲜肉" : "老鲜肉")}");
     33             #endregion
     34 
     35             #region 导入静态类(Using Static)
     36             Console.WriteLine($"之前的使用方式: {Math.Pow(4, 2)}");
     37             Console.WriteLine($"导入后可直接使用方法: {Pow(4, 2)}");
     38             #endregion
     39 
     40             #region 空值运算符(Null-conditional operators)
     41             int? iValue = 10;
     42             Console.WriteLine(iValue?.ToString());//不需要判断是否为空
     43             string name = null;
     44             Console.WriteLine(name?.ToString());
     45             #endregion
     46 
     47             #region 对象初始化器(Index Initializers)
     48             IDictionary<int, string> dictOld = new Dictionary<int, string>()
     49             {
     50                 { 1,"first"},
     51                 { 2,"second"}
     52             };
     53 
     54             IDictionary<int, string> dictNew = new Dictionary<int, string>()
     55             {
     56                 [4] = "first",
     57                 [5] = "second"
     58             };
     59 
     60             #endregion
     61 
     62             #region 异常过滤器(Exception filters)
     63             int exceptionValue = 10;
     64             try
     65             {
     66                 Int32.Parse("s");
     67             }
     68             catch (Exception e) when (exceptionValue > 1)//满足条件才进入catch
     69             {
     70                 Console.WriteLine("catch");
     71                 //return;
     72             }
     73             #endregion
     74 
     75             #region nameof表达式 (nameof expressions)
     76             Console.WriteLine(nameof(peopleTest)); //获取peopleTest这个字符串
     77             #endregion
     78 
     79             #region 在cath和finally语句块里使用await(Await in catch and finally blocks)
     80 
     81             #endregion
     82         }
     83 
     84         #region 在属性/方法里使用Lambda表达式(Expression bodies on property-like function members)
     85         public string NameFormat => string.Format("姓名: {0}", "summit");
     86         public void Print() => Console.WriteLine(Name);
     87         #endregion
     88     }
     89 
     90 
     91 
     92     public class People
     93     {
     94         public int Id { get; set; }
     95         public string Name { get; set; }
     96 
     97         public static async Task Get()
     98         {
     99             await Task.Run(() =>
    100             {
    101 
    102                 Thread.Sleep(5000);
    103                 Console.WriteLine("People.async.Get");
    104             });
    105             Console.WriteLine("People.async.Get after");
    106         }
    107     }
    108 }
    View Code

    2:c#7的新语法

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Text;
      4 
      5 namespace Test.Project
      6 {
      7     /// <summary>
      8     /// c#7新语法
      9     /// </summary>
     10     public class SharpSeven
     11     {
     12         public void Show()
     13         {
     14             #region out参数
     15             {
     16                 this.DoNoting(out int x, out int y);
     17                 Console.WriteLine(x + y);
     18 
     19                 this.DoNoting(out var l, out var m);
     20 
     21             }
     22             //Console.WriteLine(x + y);
     23             #endregion
     24 
     25             #region 模式
     26             this.PrintStars(null);
     27             this.PrintStars(3);
     28 
     29             this.Switch(null);
     30             this.Switch("RichardRichard");
     31             this.Switch("Richard");
     32             #endregion
     33 
     34             #region 元组
     35             {
     36                 var result = this.LookupName(1);
     37                 Console.WriteLine(result.Item1);
     38                 Console.WriteLine(result.Item2);
     39                 Console.WriteLine(result.Item3);
     40               
     41             }
     42             {
     43                 var result = this.LookupNameByName(1);
     44                 Console.WriteLine(result.first);
     45                 Console.WriteLine(result.middle);
     46                 Console.WriteLine(result.last);
     47 
     48                 Console.WriteLine(result.Item1);
     49                 Console.WriteLine(result.Item2);
     50                 Console.WriteLine(result.Item3);
     51             }
     52             #endregion
     53 
     54             #region 局部函数
     55             {
     56                 Add(3);
     57                 int Add(int k)//闭合范围内的参数和局部变量在局部函数的内部是可用的,就如同它们在 lambda 表达式中一样。
     58                 {
     59                     return 3 + k;
     60                 }
     61             }
     62             #endregion
     63 
     64             #region 数字分隔号
     65             long big = 100_000;
     66             #endregion
     67 
     68         }
     69 
     70         /// <summary>
     71         /// System.ValueTuple 需要安装这个nuget包
     72         /// </summary>
     73         /// <param name="id"></param>
     74         /// <returns></returns>
     75         private (string, string, string) LookupName(long id) // tuple return type
     76         {
     77             return ("first", "middle", "last");
     78         }
     79 
     80          
     81 
     82         private (string first, string middle, string last) LookupNameByName(long id) // tuple return type
     83         {
     84             return ("first", "middle", "last");
     85             //return (first: "first", middle: "middle", last: "last");
     86         }
     87 
     88         /// <summary>
     89         /// 具有模式的 IS 表达式
     90         /// </summary>
     91         /// <param name="o"></param>
     92         public void PrintStars(object o)
     93         {
     94             if (o is null) return;     // 常量模式 "null"
     95             if (!(o is int i)) return; // 类型模式 定义了一个变量 "int i"
     96             Console.WriteLine(new string('*', i));
     97         }
     98 
     99         /// <summary>
    100         /// 可以设定任何类型的 Switch 语句(不只是原始类型)
    101         /// 模式可以用在 case 语句中
    102         /// Case 语句可以有特殊的条件
    103         /// </summary>
    104         /// <param name="text"></param>
    105         private void Switch(string text)
    106         {
    107             int k = 100;
    108             switch (text)
    109             {
    110                 case "RichardRichard" when k > 10:
    111                     Console.WriteLine("RichardRichard");
    112                     break;
    113                 case "Richard" when text.Length < 10:
    114                     Console.WriteLine("Richard");
    115                     break;
    116                 case string s when s.Length > 7://模式
    117                     Console.WriteLine(s);
    118                     break;
    119                 default:
    120                     Console.WriteLine("default");
    121                     break;
    122                 case null:
    123                     Console.WriteLine("null");
    124                     break;
    125             }
    126         }
    127 
    128         private void DoNoting(out int x, out int y)
    129         {
    130             x = 1;
    131             y = 2;
    132         }
    133     }
    134 }
    View Code

      

  • 相关阅读:
    ubuntu studio
    BeanUtils包的学习
    java的GUI编程
    Mybatis之旅第三篇-SqlMapConfig.xml全局配置文件解析
    Mybatis之旅第二篇-Mapper动态代理方式
    Mybatis之旅第一篇-初识Mybatis
    Spring之旅第六篇-事务管理
    Spring之旅第五篇-AOP详解
    Spring之旅第三篇-Spring配置详解
    Spring之旅第二篇-Spring IOC概念及原理分析
  • 原文地址:https://www.cnblogs.com/loverwangshan/p/11282351.html
Copyright © 2020-2023  润新知