Take C# 8.0 for a spin
本文章为机器翻译。https://blogs.msdn.microsoft.com/dotnet/2018/12/05/take-c-8-0-for-a-spin/
以C # 8兜风
我们昨天宣布Visual Studio 2019的第一个预览(让每个开发人员在Visual Studio 2019更高效)和3(网络核心。宣布.NET核心3预览版1和开源的Windows桌面框架)。
一个令人兴奋的方面是,你可以玩一些功能来# 8 C!在这里,我要带你一个小导游通过三个新的C #功能,你可以尝试在预览。不是所有# 8特点是可用的。如果你想对所有主要功能的概述,请阅读文章# C栋8,或查看短(13分钟)的视频“什么是新的C # 8”9频道或YouTube。
准备
首先,下载并安装预览1网芯3。和Visual Studio 2019 1预览。在Visual Studio中,确保你选择的工作量”。网核心的跨平台开发”(如果你忘了,你可以把它添加后打开Visual Studio安装程序,单击“Visual Studio 2019版修改”)。
启动Visual Studio 2019预览,创建一个新的项目,并选择“控制台应用程序(.NET核心)”的项目类型。
一旦项目启动和运行,改变它的目标框架,.NET核心3(右键单击解决方案资源管理器中,项目选择属性,使用下拉菜单上的“应用程序”选项卡)。Then select C# 8.0 as the language version (on the Build tab of the project page click “Advanced…” and select “C# 8.0 (beta)”).
Now you have all the language features and the supporting framework types ready at your fingertips!
Nullable reference types
The nullable reference types feature intends to warn you about null-unsafe behavior in the code. Since we didn’t do that before, it would be a breaking change to just start now! To avoid that, you need to opt in to the feature.
Before we do turn it on, though, let’s write some really bad code:
using static System.Console;
class Program
{
static void Main(string[] args)
{
string s = null;
WriteLine($“The first letter of {s} is {s[0]}”);
}
}
If you run it you get, of course, a null reference exception. You’ve fallen into the black hole! How were you supposed to know not to dereferencesin that particular place? Well duh, because null was assigned to it on the previous line. But in real life, it’s not on the previous line, but in somebody else’s assembly running on the other side of the planet three years after you wrote your line of code.How could you have known not to write that?That’s the question that nullable reference types set out to answer! So let’s turn them on!
For a new project you should just turn them on right away. In fact I think they should probably be on by default in new projects, but we didn’t do that in the preview. The way to turn them on is to add the following line to your .csproj file, e.g. right after the LanguageVersion that was just inserted when you switched to C# 8.0 above:
true
Save the .csproj file and return to your program: What happened? You got two warnings! Each represents one “half” of the feature. Let’s look at them in turn. The first one is on the无效的in this line:
string s = null;
它抱怨你把null赋值给“非可空类型”:whaaat?!?当该功能开启空常引用类型不再受欢迎如字符串!因为,你知道的,零不是一个字符串!我们假装了面向对象编程的最后五十年,但实际上零其实不是一个对象:这就是为什么所有的爆炸,当你想把它像一个!
所以没有更多的是:空是禁止的,除非你问了。怎么做你问了吗?通过使用可空类型的引用,如字符串。后面的问号,允许信号无效:
字符串S = null;
警告消失:我们已经明确表示,这个变量保存空的意图,所以现在很好。
直到下一行代码!就行了
WriteLine($”的第一个字母,{ }是{ } [ 0 ]”);
它抱怨S进入s[0]that you may be dereferencing a null reference. And sure enough: you are! Well done, compiler! How do you fix it, though? Well that’s pretty much up to you – whichever way you would always have fixed it! Let’s try for starters to only execute the line whensis not null:
if (s != null) WriteLine($“The first letter of {s} is {s[0]}”);
The warning goes away! 为什么?Because the compiler can see that you only go to the offending code whens是不无效的.It actually does a full flow analysis, tracking every variable across every line of code to keep tabs on where it might be null and where it probably won’t be. It watches your tests and assignments, and does the bookkeeping.
Let’s try another version:
WriteLine($“The first letter of {s} is {s?[0] ?? ‘?’}”);
This uses the null conditional indexing operators?[0]which avoids the dereference and produces a null ifsis null. Now we have a nullablechar?, but the null-coalescing operator?? '?'replaces a null value with the烧焦 ‘?’。So all null dereferences are avoided. The compiler is happy, and no warnings are given.
As you can see, the feature keeps you honest while you code: it forces you to express your intent whenever you want null in the system, by using a nullable reference type. And once null is there, it forces you to deal with it responsibly, making you check whenever there’s a risk that a null value may be dereferenced to trigger a null reference exception.
Are you completely null-safe now? No. There are a couple of ways in which a null may slip through and cause a null reference exception:
If you call code that didn’t have the nullable reference types feature on (maybe it was compiled before the feature even existed), then we cannot know what the intent of that code was: it doesn’t distinguish between nullable and nonnullable – we say that it is “null-oblivious”. So we give it a pass; we simply don’t warn on such calls.
The analysis itself has certain holes. Most of them are a trade-off between safety and convenience; if we complained, it would be really hard to fix. For instance, when you writenew string[10], we create an array full of nulls, typed as non-null字符串s. We don’t warn on that, because how would the compiler keep track of you initializing all the array elements?
But on the whole, if you use the feature extensively (i.e. turn it on everywhere) it should take care of the vast majority of null dereferences.
It is definitely our intention that you should start using the feature on existing code! Once you turn it on, you may get a lot of warnings. Some of these actually represent a problem: Yay, you found a bug! Some of them are maybe a bit annoying; your code is clearly null safe, you just didn’t have the tools to express your intent when you wrote it: you didn’t have nullable reference types! For instance, the line we started out with:
string s = null;
That’s going to be super common in existing code!And as you saw, we did get a warning on the next line, too, where we tried to dereference it.So the assignment warning here is strictly speaking superfluous from a safety standpoint: It keeps you honest in new code, but fixing all occurences in existing code would not make it any safer.For that kind of situation we are working on a mode where certain warnings are turned off, when it doesn’t impact the null safety, so that it is less daunting to upgrade existing code.
Another feature to help upgrade is that you can turn the feature on or off “locally” in your code, using compiler directives#nullable enable和#nullable disable。That way you can go through your project and deal with annotations and warnings gradually, piece by piece.
To learn more about nullable reference types check out theOverview of Nullable typesand theIntroduction to nullable tutorial打开(放)docs.microsoft.com。
For a deeper design rationale, last year I wrote a postIntroducing Nullable Reference Types in C#。
If you want to immerse yourself in the day-to-day of the design work, look at theLanguage Design Noteson GitHub, or follow along as I try to put together aNullable Reference Types Specification。
范围和指标
C #越来越表现在索引数据结构。想要简单的语法切出数组的一部分,字符串或跨越?现在你可以!
去改变你的程序如下:
使用系统。收藏。通用;
使用静态系统控制台;
类节目
{
static void main(String [] args
{)
foreach(变量名在getnames())
{
WriteLine(名称);
}
}
静态IEnumerable <字符串> getnames()
{
字符串[]名字=
{
“阿基米德”、“毕达哥拉斯”、“Euclid”、“Socrates”、“Plato”
};
foreach(变量名的名字)
我们去的那点代码遍历数组的名字。修改foreach如下:
foreach(名字[ 1 … 4 ]变量名)
它看起来像我们遍历1到4名。的确,当你运行它的时候,会发生什么事情!端点独家4元,即不包括。1的4。实际上是一个范围的表达,并没有发生像在这里,作为一个索引操作部分。它有一种自己的,称为范围。只要我们想,我们可以把它变成自己的变量,它会一样工作:
范围为1。4;
foreach(var名称名称[范围
一系列表达的端点不必处理。事实上,他们是一个类型,指数非负整数转换。但你也可以创建一个指数一个新的运营商,意为“结束”。所以 1是一个从端:
foreach(名字[ 1 … ^ 1 ])变量名
本机型在阵列的每个端元,产生一个与中三个元素的数组。
范围表达式或两端开放。^ 1 …是一样的0、^ 1。1 …是一样的1、^ 0。和…是一样的0、^ 0:开始到结束。所有这些尝试看看!尝试混合和匹配“开始”和“结束”指数ES的两端范围看看会发生什么。
范围不仅仅是用来使用的索引。例如,我们计划要重载string.substring,跨。片和asspan扩展方法,以范围。那些不在这个预览网芯3虽然。
异步数据流
IEnumerable 在C #特殊作用。”ienumerables”代表各种不同的数据序列,而语言有消费和生产的特殊结构。
在我们目前的计划看,他们被通过foreach声明,这是获得一个枚举数的苦工,反复推进,提取沿途的元素,最后将枚举器。他们可以制作迭代器方法::生利他们被要求通过消费元素。都是同步的,但结果更好的准备的时候,他们的要求,或线程块!
异步和等待添加到C #处理结果,不一定要准备好当你问他们。他们可以异步等待ED,和线程可以去做其他的东西,直到他们成为可用。但这只对单一的价值观,没有序列,逐步和异步产生的时间,如从一个物联网传感器或数据流从一个服务实例的测量。
异步数据流,使异步和枚举接口在C #!让我们来看看,通过逐步“async’ifying“我们目前的计划。
首先,让我们添加另一个使用在文件的顶部指令:
使用system.threading.tasks;
现在让我们来模拟getnames做一些异步工作加在名字异步延迟生利预计起飞时间:
等待任务。延迟(1000);
产量;
当然,我们得到一个错误,你只能等待在一个异步方法所以让我们异步:
静态异步IEnumerable <字符串> getnames()
现在我们说,我们不能返回正确的类型为一个异步方法,这是公平的。但在类型列表的新候选人可以除了通常的回报任务东西IAsyncEnumerable。This is our async version ofIEnumerable!Let’s return that:
static async IAsyncEnumerable GetNames()
Just like that we’ve produced an asynchronous stream of strings! In accordance with naming guidelines, let’s renameGetNames到GetNamesAsync。
static async IAsyncEnumerable GetNamesAsync()
Now we get an error on this line in the主要方法
foreach (var name in GetNamesAsync())
Which doesn’t know how to foreach over anIAsyncEnumerable。That’s because foreach’ing over asynchronous streams requires explicit use of the等待关键词
await foreach (var name in GetNamesAsync())
It’s the version of foreach that takes an async stream and awaits every element! Of course it can only do that in an async method, so we have to make our主要method async. 幸运的是,C # 7.2增加支持:
静态异步任务主要(string [] args)
现在所有的线条都消失了,和程序的正确性。但如果你编译并运行它,你会得到一个尴尬的错误数。那是因为我们搞砸了一点,并没有得到预览。网芯3和Visual Studio 2019完全一致。具体来说,有一种类型的迭代器实现异步杠杆的不同,什么编译器期望。
您可以通过添加一个单独的源文件到你的项目中解决这个问题,含这桥码。重新编译,一切都应该是不错的。
接下来的步骤
请让我们知道你在想什么!如果你尝试这些功能,如何提高他们的想法,请使用反馈按钮在Visual Studio 2019预览。整个目的预览是有一个最后的机会,当然是正确的,基于怎样的特点发挥出在手生活的真实用户,所以请让我们知道!
快乐的黑客,
MADS托格森,C #设计领先
Take C# 8.0 for a spin的更多相关文章
- [20190419]shared latch spin count 2.txt
[20190419]shared latch spin count 2.txt --//上午测试shared latch XX模式的情况,链接:http://blog.itpub.net/267265 ...
- [20190419]shared latch spin count.txt
[20190419]shared latch spin count.txt --//昨天测试exclusive latch spin count = 20000(缺省).--//今天测试shared ...
- 使用Cocos2d-x实现微信“天天爱消除”炫耀button特效
引言Cocos2d-x引擎中有很多Action,这样可以方便的让开发者调用相应的Action去完成一些动作,例如:移动,弹跳,淡入淡出等.可在实际的开发过程中,由于游戏的需要,显然地,引擎自带的Act ...
- [应用][js+css3]3D盒子导航[PC端]
CSS3构建的3D盒子之导航应用 1.在用css3构建的盒子表面,放上iframe,来加载导航页面. 2.鼠标左键按下移动可旋转盒子,寻找想要的网址. 3.左键单机盒子表面,将全屏现实所点盒子表面的网 ...
- 能产生粒子效果的CAEmitterLayer
能产生粒子效果的CAEmitterLayer 下雪效果: // // RootViewController.m // Cell // // Copyright (c) 2014年 Y.X. All r ...
- PyOpenGL利用文泉驿正黑字体显示中文字体
摘要:在NeHe的OpenGL教程第43课源代码基础上,调用文泉驿正黑字体实现中文字体的显示 在OpenGL中显示汉字一直是个麻烦的事情,很多中文书籍的文抄公乐此不疲地介绍各种方法及其在windows ...
- cocos基础教程(8)粒子效果
简介 粒子系统是指计算机图形学中模拟特定现象的技术,它在模仿自然现象.物理现象及空间扭曲上具备得天独厚的优势,为我们实现一些真实自然而又带有随机性的特效(如爆炸.烟花.水流)提供了方便. 粒子属性 一 ...
- X86平台乱序执行简要分析(翻译为主)
多处理器使用松散的内存模型可能会非常混乱,写操作可能会无序,读操作可能会返回不是我们想要的值,为了解决这些问题,我们需要使用内存栅栏(memory fences),或者说内存屏障(memory bar ...
- NeHe OpenGL教程 第二十一课:线的游戏
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
随机推荐
- java面试题jvm字节码的加载与卸载
虚拟机把描述类的数据从class文件加载到内存,并对数据进行校验,转换分析和初始化,最终形成可以被虚拟节直接使用的JAVA类型,这就是虚拟机的类加载机制. 类从被加载到虚拟机内存到卸载出内存的生命周期 ...
- SpringBoot2.x入门:使用MyBatis
这是公众号<Throwable文摘>发布的第25篇原创文章,收录于专辑<SpringBoot2.x入门>. 前提 这篇文章是<SpringBoot2.x入门>专辑的 ...
- git push到远程新分支
获取远程代码并在本地切换到一个新分支修改后,想要 push 到远端与原来不同的新分支,可以使用下面的命令实现: git push origin 本地分支:远端希望创建的分支 上面的本地分支 是基于拉取 ...
- Linux上搭建文件浏览的web服务(创建软件仓库)(一)
软件仓库的创建方式有很多,这是一种很简单的创建方式: python -m SimpleHTTPServer 快速搭建一个http服务,提供一个文件浏览的web服务. 使用:Python SimpleH ...
- 聊聊Django应用的部署和性能的那些事儿
随着工作的深入,我越来越发现Python Web开发中有很多坑,也一直在羡慕AspNetCore和Go等的可执行文件部署和高性能,以及Spring生态的丰富,不过因为工作用了Django,生活还是要继 ...
- Lun4R-CyBRICSCTF wp
WEB Hunt (Web, Baby, 50 pts) 打断点,然后就一个一个被抓住了... 接着F12就出现了.(这个flag是白色的,藏在下面....)... RE Baby Rev 题目给了个 ...
- 用友U8API 8.9-15.0接口开发前提,选好开发方式
在用友接口开发这条路上,走走停停过了好几年.对于如何选择哪种方式,目前总结几点, 对于开发,目前可以实现的有三种方式 一.是通过用友官方提供的(EAI/API)接口 这种方式的优点 ...
- dp 数字三角形
冻龟之前 先看地龟 // // Created by snnnow on 2020/7/23 // //递归算法,除了慢其实还好 #include<iostream> #include&l ...
- Django学习路37_request属性
打印元信息,基本上都会打印出来 类字典结构的 key 键 允许重复 get 请求可以传参,但是长度有限制 最大不能超过 2K post 文件上传使用 get 参数 默认放在网址中 post 在 ...
- Python File flush() 方法
概述 flush() 方法是用来刷新缓冲区的,即将缓冲区中的数据立刻写入文件,同时清空缓冲区,不需要是被动的等待输出缓冲区写入.高佣联盟 www.cgewang.com 一般情况下,文件关闭后会自动刷 ...