你可能不知道的C#特性 - 更新到9.0
张静茹2020/05/02 软件综合 IP:江苏

转载 XXXXXXXXXXXXXXXXXXXXXXX/zenronphy/p/XXXXXXXXXXXml

目录


C# Language Design

C# Language Specification

Standard ECMA-334

  • 2002.12,包含C# 1.0,C# 1.1,C# 1.2。

  • 2006.06,包含C# 2.0。

  • 2017.12,C# 5.0版本,最新版本

Microsoft

VersionDate.NET FrameworkCLRVisual Studio
C# 1.02002.01.NET Framework 1.01.0VS 2002
C# 1.1
C# 1.2
2003.10.NET Framework 1.11.1VS 2003
C# 2.02005.11.NET Framework 2.02.0VS 2005
C# 3.02007.11.NET Framework 2.0
.NET Framework 3.0
.NET Framework 3.5
2.0VS 2008
VS 2010
C# 4.02010.04.NET Framework 44.0VS 2010
C# 5.02012.08.NET Framework 4.5
.NET Framework 4.5.1
4.0VS 2012
VS 2013
C# 6.02015.07.NET Framework 4.64.0VS 2015
C# 7.02017.03.NET Framework 4.6.24.0VS 2017
C# 7.12017.08.NET Framework 4.74.0VS 2017 v15.3
C# 7.22017.11.NET Framework 4.7.14.0VS 2017 v15.5
C# 7.32018.05.NET Framework 4.7.24.0VS 2017 v15.7
C# 8.02019.10.NET Framework 4.84.0VS 2019 v16.3

C# Language Feature

C# 1.0 - Visual Studio .NET 2002

  • Classes:类,面向对象特性

  • Structs:结构

  • Interfaces:接口

  • Events:事件

  • Properties:属性,类的成员,提供访问字段的灵活方法。

  • Delegates:委托,一种引用类型,表示对具有特定参数列表和返回类型的方法的引用,类似于函数指针。

  • Expressions:表达式

  • Statements:语句是以分号结尾的单行代码,也可以是语句块中的一系列单行语句。 语句块括在括号 {} 中,并且可以包含嵌套块。

  • Attributes:特性,为程序代码添加元数据或声明性信息,运行时,通过反射可以访问特性信息。

  • Literals:字面量,也就是不用通过构造函数来创建值

C# 1.2 - Visual Studio .NET 2003

  • Dispose in foreach:当 IEnumerator 实现 IDisposable 时,foreach 循环中生成的代码会在 IEnumerator 上调用 Dispose。

  • foreach over string specialization:foreach上的字符串特化。

C# 2.0 - Visual Studio 2005

  • Generics:泛型

  • Partial types:可以将类、结构、接口等类型定义拆分到多个文件中

  • Anonymous methods:匿名方法

  • Iterators:迭代器

  • Nullable types:可为空的值类型

  • Getter/setter separate accessibility:属性访问控制

  • Method group conversions (delegates):方法组转换,可以将声明委托代表一组方法,隐式调用

  • Co- and Contra-variance for delegates and interfaces:协变和逆变能够实现数组类型、委托类型参数的隐式引用转换。 协变保留分配兼容性,逆变则与之相反。

  • Static classes:静态类

  • Delegate inference:委托推断,将一个方法的地址传送给一个委托变量,编译器会自动检测委托变量的委托类型,然后根据委托类型创建委托实例,并把方法地址传送给委托的构造函数。

C# 3.0 - Visual Studio 2008

C# 4.0 - Visual Studio 2010

C# 5.0 - Visual Studio 2012

  • Asynchronous methods:异步方法

  • Caller info attributes:调用方信息特性,被调用时访问调用者的信息

  • foreach loop was changed to generates a new loop variable rather than closing over the same variable every time:foreach循环生成一组新变量而不是每次都隐藏旧变量

C# 6.0 - Visual Studio 2015

C# 7.0 - Visual Studio 2017

C# 7.1 - Visual Studio 2017 version 15.3

C# 7.2 - Visual Studio 2017 version 15.5

C# 7.3 - Visual Studio 2017 version 15.7

C# 8.0 - .NET Core 3.0 and Visual Studio 2019 version 16.3

  • Nullable reference types: express nullability intent on reference types with ?notnull constraint and annotations attributes in APIs, the compiler will use those to try and detect possible null values being dereferenced or passed to unsuitable APIs.

  • Default interface members: interfaces can now have members with default implementations, as well as static/private/protected/internal members except for state (ie. no fields).

  • Recursive patterns: positional and property patterns allow testing deeper into an object, and switch expressions allow for testing multiple patterns and producing corresponding results in a compact fashion.

  • Async streamsawait foreach and await using allow for asynchronous enumeration and disposal of IAsyncEnumerable collections and IAsyncDisposable resources, and async-iterator methods allow convenient implementation of such asynchronous streams.

  • Enhanced using: a using declaration is added with an implicit scope and using statements and declarations allow disposal of ref structs using a pattern.

  • Ranges and indexes: the i..j syntax allows constructing System.Range instances, the ^k syntax allows constructing System.Index instances, and those can be used to index/slice collections.

  • Null-coalescing assignment??= allows conditionally assigning when the value is null.

  • Static local functions: local functions modified with static cannot capture this or local variables, and local function parameters now shadow locals in parent scopes.

  • Unmanaged generic structs: generic struct types that only have unmanaged fields are now considered unmanaged (ie. they satisfy the unmanaged constraint).

  • Readonly members: individual members can now be marked as readonly to indicate and enforce that they do not modify instance state.

  • Stackalloc in nested contextsstackalloc expressions are now allowed in more expression contexts.

  • Alternative interpolated verbatim strings@$"..." strings are recognized as interpolated verbatim strings just like $@"...".

  • Obsolete on property accessors: property accessors can now be individually marked as obsolete.

  • Permit t is null on unconstrained type parameter

C# 9.0

  • Records and Pattern-based With-Expression:Records是一种轻量级的不可变类型。

  • Type Classes:Type Classes允许您在类型上添加一组操作,但不实现它。

  • Dictionary Literals:字典字面量。

  • Params Span<T>:允许用params修饰Span<T>。

  • Allow no-arg constructor and field initializers in struct declarations:结构体允许使用无参构造函数。

  • Native-Sized Number Types:原生大小的数字类型(nint,nuint,nfloat等)'n'表示native(原生),该特性允许声明一个32位或64位的数据类型,这取决于操作系统的平台类型。

  • Fixed Sized Buffers:固定大小的缓冲区。

  • Uft8 string literals:新的字符串类型UTF8String。

  • Base(T):解决默认接口方法中的覆盖冲突问题

Reference


来自:计算机科学 / 软件综合
0
已屏蔽 原因:{{ notice.reason }}已屏蔽
{{notice.noticeContent}}
~~空空如也

想参与大家的讨论?现在就 登录 或者 注册

所属专业
所属分类
上级专业
同级专业
张静茹
进士 学者 机友 笔友
文章
138
回复
1857
学术分
1
2010/12/30注册,4天0时前活动
暂无简介
主体类型:个人
所属领域:无
认证方式:手机号
IP归属地:未同步
文件下载
加载中...
{{errorInfo}}
{{downloadWarning}}
你在 {{downloadTime}} 下载过当前文件。
文件名称:{{resource.defaultFile.name}}
下载次数:{{resource.hits}}
上传用户:{{uploader.username}}
所需积分:{{costScores}},{{holdScores}}下载当前附件免费{{description}}
积分不足,去充值
文件已丢失

当前账号的附件下载数量限制如下:
时段 个数
{{f.startingTime}}点 - {{f.endTime}}点 {{f.fileCount}}
视频暂不能访问,请登录试试
仅供内部学术交流或培训使用,请先保存到本地。本内容不代表科创观点,未经原作者同意,请勿转载。
音频暂不能访问,请登录试试
支持的图片格式:jpg, jpeg, png
插入公式
评论控制
加载中...
文号:{{pid}}
投诉或举报
加载中...
{{tip}}
请选择违规类型:
{{reason.type}}

空空如也

加载中...
详情
详情
推送到专栏从专栏移除
设为匿名取消匿名
查看作者
回复
只看作者
加入收藏取消收藏
收藏
取消收藏
折叠回复
置顶取消置顶
评学术分
鼓励
设为精选取消精选
管理提醒
编辑
通过审核
评论控制
退修或删除
历史版本
违规记录
投诉或举报
加入黑名单移除黑名单
查看IP
{{format('YYYY/MM/DD HH:mm:ss', toc)}}