深度解析.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的新语法
如图:

具体代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static System.Math; namespace Test.Project
{
/// <summary>
/// C#6 新语法
/// </summary>
public class SharpSix
{
#region 自动属性初始化(Auto-property initializers)
public string Name { get; set; } = "summit";
public int Age { get; set; } = ;
public DateTime BirthDay { get; set; } = DateTime.Now.AddYears(-);
public IList<int> AgeList
{
get;
set;
} = new List<int> { , , , , };
#endregion public void Show(People peopleTest)
{
#region 字符串嵌入值(String interpolation)
Console.WriteLine($"年龄:{this.Age} 生日:{this.BirthDay.ToString("yyyy-MM-dd")}");
Console.WriteLine($"年龄:{{{this.Age}}} 生日:{{{this.BirthDay.ToString("yyyy -MM-dd")}}}"); Console.WriteLine($"{(this.Age <= 22 ? "小鲜肉" : "老鲜肉")}");
#endregion #region 导入静态类(Using Static)
Console.WriteLine($"之前的使用方式: {Math.Pow(4, 2)}");
Console.WriteLine($"导入后可直接使用方法: {Pow(4, 2)}");
#endregion #region 空值运算符(Null-conditional operators)
int? iValue = ;
Console.WriteLine(iValue?.ToString());//不需要判断是否为空
string name = null;
Console.WriteLine(name?.ToString());
#endregion #region 对象初始化器(Index Initializers)
IDictionary<int, string> dictOld = new Dictionary<int, string>()
{
{ ,"first"},
{ ,"second"}
}; IDictionary<int, string> dictNew = new Dictionary<int, string>()
{
[] = "first",
[] = "second"
}; #endregion #region 异常过滤器(Exception filters)
int exceptionValue = ;
try
{
Int32.Parse("s");
}
catch (Exception e) when (exceptionValue > )//满足条件才进入catch
{
Console.WriteLine("catch");
//return;
}
#endregion #region nameof表达式 (nameof expressions)
Console.WriteLine(nameof(peopleTest)); //获取peopleTest这个字符串
#endregion #region 在cath和finally语句块里使用await(Await in catch and finally blocks) #endregion
} #region 在属性/方法里使用Lambda表达式(Expression bodies on property-like function members)
public string NameFormat => string.Format("姓名: {0}", "summit");
public void Print() => Console.WriteLine(Name);
#endregion
} public class People
{
public int Id { get; set; }
public string Name { get; set; } public static async Task Get()
{
await Task.Run(() =>
{ Thread.Sleep();
Console.WriteLine("People.async.Get");
});
Console.WriteLine("People.async.Get after");
}
}
}
2:c#7的新语法
using System;
using System.Collections.Generic;
using System.Text; namespace Test.Project
{
/// <summary>
/// c#7新语法
/// </summary>
public class SharpSeven
{
public void Show()
{
#region out参数
{
this.DoNoting(out int x, out int y);
Console.WriteLine(x + y); this.DoNoting(out var l, out var m); }
//Console.WriteLine(x + y);
#endregion #region 模式
this.PrintStars(null);
this.PrintStars(); this.Switch(null);
this.Switch("RichardRichard");
this.Switch("Richard");
#endregion #region 元组
{
var result = this.LookupName();
Console.WriteLine(result.Item1);
Console.WriteLine(result.Item2);
Console.WriteLine(result.Item3); }
{
var result = this.LookupNameByName();
Console.WriteLine(result.first);
Console.WriteLine(result.middle);
Console.WriteLine(result.last); Console.WriteLine(result.Item1);
Console.WriteLine(result.Item2);
Console.WriteLine(result.Item3);
}
#endregion #region 局部函数
{
Add();
int Add(int k)//闭合范围内的参数和局部变量在局部函数的内部是可用的,就如同它们在 lambda 表达式中一样。
{
return + k;
}
}
#endregion #region 数字分隔号
long big = 100_000;
#endregion } /// <summary>
/// System.ValueTuple 需要安装这个nuget包
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private (string, string, string) LookupName(long id) // tuple return type
{
return ("first", "middle", "last");
} private (string first, string middle, string last) LookupNameByName(long id) // tuple return type
{
return ("first", "middle", "last");
//return (first: "first", middle: "middle", last: "last");
} /// <summary>
/// 具有模式的 IS 表达式
/// </summary>
/// <param name="o"></param>
public void PrintStars(object o)
{
if (o is null) return; // 常量模式 "null"
if (!(o is int i)) return; // 类型模式 定义了一个变量 "int i"
Console.WriteLine(new string('*', i));
} /// <summary>
/// 可以设定任何类型的 Switch 语句(不只是原始类型)
/// 模式可以用在 case 语句中
/// Case 语句可以有特殊的条件
/// </summary>
/// <param name="text"></param>
private void Switch(string text)
{
int k = ;
switch (text)
{
case "RichardRichard" when k > :
Console.WriteLine("RichardRichard");
break;
case "Richard" when text.Length < :
Console.WriteLine("Richard");
break;
case string s when s.Length > ://模式
Console.WriteLine(s);
break;
default:
Console.WriteLine("default");
break;
case null:
Console.WriteLine("null");
break;
}
} private void DoNoting(out int x, out int y)
{
x = ;
y = ;
}
}
}
深度解析.NetFrameWork/CLR/C# 以及C#6/C#7新语法的更多相关文章
- [WebKit内核] JavaScript引擎深度解析--基础篇(一)字节码生成及语法树的构建详情分析
[WebKit内核] JavaScript引擎深度解析--基础篇(一)字节码生成及语法树的构建详情分析 标签: webkit内核JavaScriptCore 2015-03-26 23:26 2285 ...
- [WebKit内核] JavaScriptCore深度解析--基础篇(一)字节码生成及语法树的构建
看到HorkeyChen写的文章<[WebKit] JavaScriptCore解析--基础篇(三)从脚本代码到JIT编译的代码实现>,写的很好,深受启发.想补充一些Horkey没有写到的 ...
- 第37课 深度解析QMap与QHash
1. QMap深度解析 (1)QMap是一个以升序键顺序存储键值对的数据结构 ①QMap原型为 class QMap<K, T>模板 ②QMap中的键值对根据Key进行了排序 ③QMap中 ...
- Deep Learning模型之:CNN卷积神经网络(一)深度解析CNN
http://m.blog.csdn.net/blog/wu010555688/24487301 本文整理了网上几位大牛的博客,详细地讲解了CNN的基础结构与核心思想,欢迎交流. [1]Deep le ...
- (转载)(收藏)OceanBase深度解析
一.OceanBase不需要高可靠服务器和高端存储 OceanBase是关系型数据库,包含内核+OceanBase云平台(OCP).与传统关系型数据库相比,最大的不同点, 是OceanBase是分布式 ...
- Kafka深度解析
本文转发自Jason’s Blog,原文链接 http://www.jasongj.com/2015/01/02/Kafka深度解析 背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅 ...
- java内存分配和String类型的深度解析
[尊重原创文章出自:http://my.oschina.net/xiaohui249/blog/170013] 摘要 从整体上介绍java内存的概念.构成以及分配机制,在此基础上深度解析java中的S ...
- Unity加载模块深度解析(Shader)
作者:张鑫链接:https://zhuanlan.zhihu.com/p/21949663来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 接上一篇 加载模块深度解析(二 ...
- Unity加载模块深度解析(网格篇)
在上一篇 加载模块深度解析(一)中,我们重点讨论了纹理资源的加载性能.这次,我们再来为你揭开其他主流资源的加载效率. 这是侑虎科技第53篇原创文章,欢迎转发分享,未经作者授权请勿转载.同时如果您有任何 ...
随机推荐
- Jinkins自动构建
Jinkins自动构建 1.项目添加 点击左侧操作栏“新建”,填写项目基础信息,如下图: 2. General配置 2.1 丢弃旧的构建 注:此处勾选丢弃旧的构建,默认天数为1,最大个数建议填写3-5 ...
- Java jdom解析xml文件带冒号的属性
Java jdom解析xml文件带冒号的属性 转载请标明出处: https://dujinyang.blog.csdn.net/article/details/99644824 本文出自:[奥特曼超人 ...
- 在Rust中使用C语言的库功能
主要是了解unsafe{}语法块的作用. #[repr(C)] #[derive(Copy, Clone)] #[derive(Debug)] struct Complex { re: f32, im ...
- 02vuex-modules
01===> module的理解:将一个大的系统进行拆分 拆分成若干个细的模块 给个模块都有自己的 state mutations 等属性 这样可以在自己的小模块中进行修改 方便维护 modul ...
- ACM-冒泡排序
将多组输入数据进行冒泡排序,并去除相同的数据 #include <iostream> #include <vector> using namespace std; void R ...
- Monkey小白入门篇
一.monkey简介 中文名:猴子 职业:压力测试小工具 用途:对待测Android应用程序进行压力测试,测试app是否会crash Android官方描述: The Monkey is a prog ...
- 解决问题:Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean. 注释掉, ...
- jTopo介绍(一)
jTopo(Javascript Topology library)是一款完全基于HTML5 Canvas的关系.拓扑图形化界面开发工具包.jTopo关注于数据的图形展示,它是面向开发人员的,需要进行 ...
- Class的使用,构造方法,实例属性和实例方法,静态属性和静态方法,this和super关键字,类的继承
s6新增了一种定义对象实例的方法,Class(类)这个概念,作为对象的模板.class可以看作只是一个语法糖,通过class关键字,可以定义类.让对象原型的写法更加清晰.更像面向对象编程的语法. 一. ...
- postman(十二):发送携带md5签名、随机数等参数的请求
想起来之前在借助百度翻译接口做翻译小工具的时候,需要把参数进行md5加密后再传输. 而在平时的接口测试工作中难免会遇到类似这种请求参数,比如md5加密.时间戳.随机数等等.固然可以先计算出准确的参数, ...