码迷,mamicode.com
首页 > Windows程序 > 详细

C#各版本新功能 C#8.0

时间:2020-03-19 15:18:01      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:面向   quic   结构   lazy   ack   ado   string   div   tab   

ReadOnly 成员

默认接口方法

接口里面的方法都是虚方法,字类不用写override 就能对其进行覆盖;

这与抽象类里面的方法是不同的,接口管理的更加随意;以后都可以用面向接口开发了;

若是同时继承了接口1跟接口2,接口1,2都实现了方法 TurnOnFor

    public interface Interface1
    {
        public void TurnOnFor(int duration)
        {
            Task.Delay(duration);
            Console.WriteLine("我是接口 Interface1 里面的原始方法");
        }
    }

    public interface Interface2: Interface1
    {
        public void TurnOnFor(int duration)
        {
        }
    }

   public class Child : Interface2
    {
    }

    Interface1 child = new Child();

    child.TurnOnFor(11); //调用接口1中的方法

    Interface2 child = new Child();

    child.TurnOnFor(11); //调用接口2中的方法

模式匹配增强

Using 声明 升级

  using() //以前
{}

//现在

using var file =***

//方法的最后会自动帮你释放

静态本地函数

添加 static 修饰符,以确保本地函数不会从封闭范围捕获(引用)任何变量

int M () {     int y = 5 ;     int x = 7 ;     return Add(x, y);

static int Add ( int left, int right ) => left + right; }

可处置的ref结构

可为空的引用类型

异步流

索引和范围

var words = new string[]
{
                // index from start    index from end
    "The",      // 0                   ^9
    "quick",    // 1                   ^8
    "brown",    // 2                   ^7
    "fox",      // 3                   ^6
    "jumped",   // 4                   ^5
    "over",     // 5                   ^4
    "the",      // 6                   ^3
    "lazy",     // 7                   ^2
    "dog"       // 8                   ^1
};              // 9 (or words.Length) ^0

var quickBrownFox = words[1..4];
var lazyDog = words[^2..^0];
var allWords = words[..]; // contains "The" through "dog".
var firstPhrase = words[..4]; // contains "The" through "fox"
var lastPhrase = words[6..]; // contains "the", "lazy" and "dog"

Null合并赋值

C# 8.0 引入了 null 合并赋值运算符 ??= 。 ok

仅当左操作数计算为 null 时,才能使用运算符 ??= 将其右操作数的值分配给左操作数 ok

非托管构造类型

嵌套表达式中的StackAlloc

内插逐字字符串的增强功能 ok

解释说明: $ 和 @,这两个符号谁在前面都可以了

C#各版本新功能 C#8.0

标签:面向   quic   结构   lazy   ack   ado   string   div   tab   

原文地址:https://www.cnblogs.com/maanshancss/p/3992333c4e624cbbbf950dfde75d9d2a.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!