重庆熊猫 Loading

.NET教程 - .NET基础介绍(.NET Basic)

更新记录
转载请注明出处:
2022年9月27日 发布。
2022年9月26日 从笔记迁移到博客。

.NET介绍

.NET概念(.NET是什么)

.Net泛指Microsoft公司的一种技术

.Net泛指一个平台

.Net泛指一个标准:.Net Standard

.Net泛指一个具体框架实现:.Net Framework、Mono、.Net Core

.NET与其他软件间的关系

image

.NET 统一平台:
image

.NET 版本

.NET 旧版本发行时间

比较重要的时间节点:

2002年发布第一个版本

2016年发布.NET Core 1.0

2019年发布.NET Core 3.0
image

.NET 新版本开发计划

image

简要总结:

.NET Core 3.0在2019年九月发布

.NET Core 3.1在2019年十一月发布,这是一个Long Term Support版本

.NET 5.0在2020年发布,这是一个General Availability版本

.NET 6.0在2021年十一月发布,这是一个Long Term Support版本

注意:偶数版本号表示稳定版本

.NET Core版本发布节点

Version Released Edition Published
.NET Core RC1 November 2015 First March 2016
.NET Core 1.0 June 2016
.NET Core 1.1 November 2016
.NET Core 1.0.4 and .NET Core 1.1.1 March 2017 Second March 2017
.NET Core 2.0 August 2017
.NET Core for UWP in Windows 10 Fall Creators Update October 2017 Third November 2017
.NET Core 2.1 May 2018
.NET Core 2.2 December 2018
.NET Core 3.0 September 2019 Fourth October 2019
.NET Core 3.1 (LTS) November 2019
.NET 5.0 2020
.NET 6.0 November 2021

.NET 不同版本支持时间

image

.NET 5介绍

.NET框架重写(rewrite),将.NET框架 变为真正的跨平台框架

.NET Core 将.NET与Windows的紧密联系脱钩

.NET Core框架 和 .NET Framework框架 在.NET 5合并

.NET 5特点

跨平台支持,支持跨平台开发、部署 (各软件工具都支持,不算啥特点)

性能 优于 .NET 4.x Framework (这不废话嘛,重写了原有的框架的嘛)

支持独立部署,可以自包含runtime (这个必须赞,可以自包含打包框架发布)

命令行支持程度 优于 .NET Framework (毕竟跨平台的嘛)

基本上 兼容 .NET 4.x Framework (重写向后兼容)

支持容器化部署 (这个必须有,现在必须有)

.NET的优劣

优点:

​ 强大的基础类库BCL

​ 支持多种语言,语言只要符合规范,可以互相操作

​ 部署简单

缺点:

​ 必须要.NET插件才能运行程序(使用.NET Core自包含部署不存在)

​ .NET版本存在不兼容问题(使用.NET Core自包含部署不存在)

.NET 官方示例代码

https://docs.microsoft.com/zh-cn/samples/browse/

.NET Core开发和部署支持的平台

三大主流平台都支持,没啥好说的

主要是看看支持的最低的系统的最低版本

https://github.com/dotnet/core/blob/master/os-lifecycle-policy.md

.NET版本支持的生命周期(supportability schedule)

https://dotnet.microsoft.com/platform/support/policy/dotnet-core

image

.NET 5的新特征(new features)

统一了所有.NET基类库(Unified .NET with Single Base Class Library)

Previously, Xamarin apps (mobile apps) were based on the Mono BCL

but this has now moved into .NET 5 with improved compatibility as an outcome

多平台本地支持(Multi-Platform Native Apps)

A single project will be able to target multiple platforms

If you use a UI element

.NET will handle this appearing as a control native to the platform

云原生(Cloud Native)

Current .NET Code will certainly run in the cloud

but further steps will be taken towards labeling .NET a cloud-native framework

This includes a reduced footprint for easier use in containers and single file executables

so you don't need the .NET runtime to be installed

and aligning the cloud story and the local developer experience

so they are at feature parity

新增Blazor Web程序集(Blazor WebAssembly)

.NET Core 3.1 introduced Blazor apps that were rendered server-side

With .NET 5, they can be rendered client-side, enabling offline and standalone apps

The goal is that the code is close to identical

so it will be easy to switch from one hosting model to the other

多平台Web程序(Multi-Platform Web Apps)

Blazor apps was originally conceived as

a vehicle for web apps and works great in a browser

The goal is that this will work equally great for a mobile device

or a native desktop application

其他功能持续改进(Continuous Improvements)

Faster algorithms in the BCL

container support in the runtime

support for HTTP3, and other tweaks

.NET 5版本号的意义

Major.Minor.BuildPatch

主版本.次版本.构建号补丁号
image

.NET SDK & Runtime官方下载地址

https://dotnet.microsoft.com/download

.NET 5组成

Common Language Runtime ,CoreCLR (语言运行时)

Base Class Libraries ,BCL,CoreFX (类库)

Language compilers ,Roslyn (语言编译器)

SDK Tools and the “dotnet” app host (SDK工具和应用主机,应用主机用于处理CLI)
image

.Net组成

.NET = 运行时(CLR) + 类库(BCL)

CLR(Command Language Runtime,公共语言运行时)

BCL(Base Class Library,基础类库)也叫FCL(Framework Class Library)

image

.NET 技术实现对比

注意:所有的.NET技术实现最终会汇成.NET 5
image

技术(Technology) 描述(Description) Host OSes
.NET Core Modern feature set, full C# 8.0 support, port existing and create new Windows and Web apps and services Windows, macOS, Linux
.NET Framework Legacy feature set, limited C# 8.0 support, maintain existing applications Windows only
Xamarin+Mono Mobile apps only Android, iOS, macOS
UWP Windows Store Application Windows only

注意:.NET Core 3 是 .NET Core 2.2 和 .NET Framework的继承

image

image

.NET体系结构图

image

相关名词解释

CIL(Common Intermediate Language,公共中间语言)

CIL是微软.NET平台上衍生出的一门中间语言

.NET平台上的各种高级语言(如C#,VB,F#)的编译器会将各自的代码转化为CIL

C#源码最终被 编译器 编译 成 CIL

程序集的主要内容就是:CIL(Common Intermediate Language,公共中间语言)

程序集的外在文件类型:可执行的文件(.exe)、库(.dll)

使用CIL的优点:

​ 语言优势:可以使用自己喜欢的语言,可以发挥各种语言的优势

​ 平台优势:实现一次编写多平台运行,不同的语言的类库可以相互调用

使用CIL的缺点:

​ 依赖.NET 运行时、效率不如编译语言效率高

程序集包含内容:

程序代码生成的CIL、程序中使用的类型的元数据、引用其他程序集的元数据
image

CIL代码直到它被调用时才会被JIT即时编译成本机代码,然后再运行
image

程序集中的可执行代码只在需要的时候由JIT编译器即时编译

然后就会被缓存起来以备在后来的程序中执行

一旦CIL被编译成本机代码,CLR就会在该代码运行的时候管理它,比如像内存管理、异常管理

托管代码 和 非托管代码 概念:

被CLR管理的代码称为 托管代码(managed code)

不被CLR管理的代码称为 非托管代码(unmanaged code)

可以使用Ngen工具,在客户机器上把提前把程序集转换为当前机器的本机代码,免除以后的JIT编译,以提高运行速度

CLR(Command Language Runtime,公共语言运行时)

CLR(Command Language Runtime,公共语言运行时)

注意:在.NET中叫CLR,在.NET Core中叫CoreCLR

作用:负责托管程序的执行

CLR(Common Language Runtime)包含内容:

  • 即时编译器(Just-in-Time,JIT)
  • 内存管理(Memory Manager)
  • GC管理(Garbage Collector,垃圾回收器)
  • 线程管理(Threading Management)
  • 异常管理(Exception Management)
  • 反射支持(Reflection)
  • 基础.NET类型(Base .NET types)
  • Common Language Specification (CLS)
  • Common Type System (CTS)

CLR的内部组成简要示意图:
image

CLR在软件运行时示意图:
image

CLR工作内部结构图:
image

CLR在两次编译中的位置:
image

CLR主要功能:

Language interoperability: Interoperability between different source languages. This is possible because the language compilers translate each source language to the same intermediate language (CIL).

Type safety: Checks for conversion between types, ensuring that only conversions between compatible types will occur. This helps prevent the occurrence of buffer overruns, a leading cause of security vulnerabilities.

Code access security: Certification that the assembly developer’s code has permission to execute on the computer.

Garbage collection: Memory management that automatically de-allocates memory previously allocated by the runtime.

Platform portability: Support for potentially running the same assembly on a variety of operating systems. One obvious restriction is that no platform-dependent libraries are used; therefore, platform-dependent idiosyncrasies need to be worked out separately.

Base Class Library (BCL): Provides a foundation of code that developers can depend on (in all .NET frameworks) so that they do not have to develop the code themselves.

CLI(Common Language Infrastructure,公共语言基础结构)

公共语言基础结构(Common Language Infrastructure,CLI)

描述了整个.NET的架构组成:
image

The Virtual Execution System

The Common Intermediate Language

The Common Type System

The Common Language Specification

Metadata

The framework

CTS(Common Type System,公共类型系统)

公共类型系统(Common Type System)

.Net支持的所有类型和编程结构描述

公共类型系统是一种规范,描述CLR支持的数据类型和编程结构,以及元数据的表示

作用在于可以实现CLR的一种规范

CLS(Common Language Specification,公共语言规范)

CLS(Common Language Specification,公共语言规范)

每种语言都用各自不同的特性,为了能让各种语言都可以在.NET上互相调用和协作

每种语言必须遵守一个共同的标准,CLI就是这种标准

公共语言规范是公共类型系统的一个子集,也是一种规范

作用是可以让各种语言可以互相操作和共享

.Net中CTS的子集,支持所有.Net上的编程语言

attribute at the bottom of this file and set its value to true

[assembly: CLSCompliant(true)]

GAC (Global Assembly Cache)

The location for GAC is “C:\Windows\assembly”

CAS (Code Access Security)

BCL(Base Class Library)

BCL(Base Class Library)公共基础类库

基本的类型定义、文件操作、序列化等

各种类相互组合又可以组成一定的框架,比如:

Winform、Console、WPF、ASP.NET、ADO.NET、WCF、WF、Windows Service

App Domain

image

Strong Assemblies

image

Version Number 组成

  1. Major Version
  2. Minor Version
  3. Build Number
  4. Revision Number

在程序集中设置

[assembly: AssemblyVersion("3.2.\*")]

Assembly Culture

[assembly: AssemblyCulture(“en”)]

image

Public Key Token

[assembly: AssemblyKeyFile(“C:\\MyKeyFile.snk”)]

.NET Standard标准

说明

目前.NET有四种官方支持的实现:

.NET Core: 面向跨平台(cross-platform)应用 和 新应用(new apps)

.NET Framework: 面向遗留项目(legacy apps)

Xamarin: 面向移动应用(mobile apps)

​ UWP 面向Windows 商店应用

为了编写一个代码可以在多种实现上都可以使用,所以指定了一个通用的标准

.NET Standard标准不是一个实现,只是一个标准规定,规定了需要实现那些API

官方文档:https://docs.microsoft.com/zh-cn/dotnet/standard/net-standard

.NET Standard标准版本越高,可使用的 API 就越多

.NET Standard标准版本越低,可实现它的平台就越多

使用.NET Standard标准编写的代码

可以在多个.NET平台(.Net Framework、.Net Core、Xamarin)上运行

.NET Standard标准与各个实现的对应关系
image

项目中设置.Net Standard

在项目配置文件.csproj中添加

<PropertyGroup>
   <TargetFramework>netstandard2.0</TargetFramework>
<PropertyGroup>

.Net Standard实现

在编译的程序集中会引入netstandard.dll

该程序集不包含代码实现,只用于将具体的操作转发到其他具体的程序集

实现跨多个.Net实现平台运行托管代码

框架与C#语言的版本(Framework and C# Language Versions)

.NET Core SDK Roslyn C#
1.0.4 2.0 - 2.2 7.0
1.1.4 2.3 - 2.4 7.1
2.1.2 2.6 - 2.7 7.2
2.1.200 2.8 - 2.10 7.3
3.0 3.0 - 3.3 8.0

image

在创建项目的时候,选择的 框架版本 会自动对应着 C#语言的版本

.NET Core 3.x 和.NET Standard 2.1 默认使用C# 8

.NET Core 2.x、.NET Framework、.NET Standard 2.0默认使用 C# 7.3

.NET可移植性分析器

.NET Portability Analyzer

使用该工具可以快速分析代码的各执行平台(.NET、.NET Core、Mono等)的兼容性

Visual Studio扩展下载

https://marketplace.visualstudio.com/items?itemName=ConnieYau.NETPortabilityAnalyzer

官方文档

https://docs.microsoft.com/en-us/dotnet/standard/analyzers/portability-analyzer

.NET代码编译和执行过程

image

.NET平台提供功能与命名空间对应关系

基础类型                          System

文本处理                          System.Text

LINQ查询                         System.Linq

数据处理                          System.Data

程序诊断                          System.Diagnostics

并发与异步                       System.Threading

流与IO                            System.IO

网络                               System.Net

网络-邮件                         System.Net.Mail

网络-Http                         System.Net.Http

网络-Socket                        System.Net.Sockets

XML                                System.Xml

XML-LIINQ                         System.Xml.Linq

XML-序列化                        System.Xml.Serialization

序列化                             System.Runtiime.Serialization

反射                              System.Reflection

动态编程                          System.Dynamic

安全                              System.Security

安全-认证                          System.Security.Permissions

安全-策略                         System.Security.Policy

安全-加密                         System.Security.Cryptography

COM互操作                       System.Runtime.InteropServices

全球化                            System.Globalization

基于.NET平台的 框架 和 功能

框架 功能
WinForms Windows PC Software
WPF Windows PC Software
ADO.NET Database Development
ASP.NET WebForms Web Development
ASP.NET MVC Web Development
Asp.Net Web API Web API
WCF NetWork
WF WorkFlow
Entity Framework Database Development
Xamarin Mobile Software
UWP Windows Store商店软件

.NET 反编译工具

ILSpy

.NET Core默认安装路径

Windows系统 %ProgramFiles%\dotnet

Linux系统 /usr/bin/dotnet

Runtime的作用

handles memory allocation and de-allocation

type checking

interoperability with other languages

cross-platform execution

support for programming metadata

安装.NET SDK

下载安装.NET SDK

官方下载地址:https://dotnet.microsoft.com/download

选择对应的系统版本
image

下载完成后点击安装即可
image

完成安装后,会提示我们以下内容都安装好了
• .NET Core SDK 3.1.100
• .NET Core Runtime 3.1.0
• ASP.NET Core Runtime 3.1.0
• .NET Core Windows Desktop Runtime 3.1.0
image

运行基本CLI命令

下载完成后,我们进入按下WIN+R打开运行窗口,输入CMD打开命令行
image

然后输入dotnet查看命令行是否工作正常
image

如果没有安装成功,翻一翻.NET Core官方文档

BCL(Base Class Library)

说明

几乎所有.NET的功能都是通过大量托管类型来实现的

这些类型被组织成分层的名称空间(hierarchical namespaces)

并打包成一组程序集,这些程序集与CLR一起构成.NET核心平台

BCL提供的主要功能

System Types(系统预定义类型)

最基础最核心的类型都放在System命名空间中

比如:

built-in types、the Exception base class

Enum、Array、Delegate base classes

Nullable、Type、DateTime、TimeSpan、Guid

Math、Random、Convert、BitConverter

IFormattable、IComparable、IDisposable

Text Processing(文本处理)

主要定义在System.Text命名空间中

比如:StringBuilder类型

System.Text.RegularExpressions命名空间下的类型用于处理正则表达式

Collections(集合)

主要是基于list-和dictionary-的集合

主要命名空间

System.Collections // Nongeneric collections

System.Collections.Generic // Generic collections

System.Collections.Specialized // Strongly typed collections

System.Collections.ObjectModel // Bases for your own collections

System.Collections.Concurrent // Thread-safe collection

Querying(数据查询)

主要命名空间:

System.Linq // LINQ to Objects and PLINQ

System.Linq.Expressions // For building expressions manually

System.Xml.Linq // LINQ to XML

XML & JSON

主要命名空间:

System.Xml // XmlReader, XmlWriter

System.Xml.Linq // The LINQ to XML DOM

System.Xml.Schema // Support for XSD

System.Xml.Serialization // Declarative XML serialization for .NET types

System.Xml.XPath // XPath query language

System.Xml.Xsl // Stylesheet support

System.Text.Json // JSON reader/writer and document object model

Diagnostics(诊断)

主要命名空间:

System.Diagnostics

Concurrency and Asynchrony(并行和异步)

主要命名空间:

System.Threading

System.Threading.Tasks

Streams and I/O(流和IO)

主要命名空间:

System.IO

Networking(网络处理)

主要命名空间:

System.Net

System.Net.Http // HttpClient

System.Net.Mail // For sending mail via SMTP

System.Net.Sockets // TCP, UDP, and IP

Serialization(序列化)

主要命名空间:

System.Runtime.Serialization

System.Xml.Serialization

System.Text.Json

Assemblies, Re€ection, and Attributes(集合、反射、特性)

主要命名空间:

System

System.Reflection

System.Reflection.Emit

Dynamic Programming(动态编程)

主要命名空间:

System.Dynamic

Cryptography(加密解密)

主要命名空间:

System.Security

System.Security.Cryptography

Native and COM Interoperability(本机和COM互操作性)

主要命名空间:

System.Runtime.InteropServices

The Roslyn Compiler

The C# compiler itself is written in C#

posted @ 2022-09-27 08:29  重庆熊猫  阅读(1091)  评论(0编辑  收藏  举报