原文链接:https://blog.okyrylchuk.dev

原文作者:Oleg Kyrylchuk

译: 等天黑

常量的内插字符串

C# 10 允许使用在常量字符串初始化中使用插值, 如下

const string name = "Oleg";
const string greeting = $"Hello, {name}."; Console.WriteLine(greeting);
// Output: Hello, Oleg.

扩展属性模式

从 C# 10 开始,您可以在适当的模式中引用嵌套的属性或字段, 属性模式变得更具可读性并且需要更少的大括号。

Person person = new()
{
Name = "Oleg",
Location = new() { Country = "PL" }
}; if (person is { Name: "Oleg", Location.Country: "PL" })
{
Console.WriteLine("It's me!");
} class Person
{
public string Name { get; set; }
public Location Location { get; set; }
} class Location
{
public string Country { get; set; }
}

如果Location为null,则不会匹配模式并返回false。

文件范围的命名空间

C# 10 引入了一种新的命名空间声明方式 - 文件范围的命名空间,减少一个大括号,代码结构更简洁。

namespace FileScopedNamespace;

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}

全局 Using

一次引用,全局通用

global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading.Tasks; List<int> list = new() { 1, 2, 3, 4 };
int sum = list.Sum();
Console.WriteLine(sum); await Task.Delay(1000);

同一个解构中的赋值和声明

C# 10 可以在同一个解构中进行赋值和声明。

var rgb = (255, 100, 30);

// Initialization & assignment
int r;
(r, int g, int b) = rgb; Console.WriteLine($"RGB: {r}, {g}, {b}");
// Output: RGB: 255, 100, 30

Record 类型重写 ToString() 时支持密封

Product product = new() { Name = "Bread" };
Console.WriteLine(product.ToString());
// Output: Bread public record Product
{
public string Name { get; init; } public sealed override string ToString()
{
return Name;
}
}

Record Struct

C# 10 支持 record struct

Person me = new() { FirstName = "Oleg", LastName = "Kyrylchuk" };

Console.WriteLine(me);
// Output: Person { FirstName = Oleg, LastName = Kyrylchuk } Person otherPerson = me with { FirstName = "John" };
Console.WriteLine(otherPerson);
// Output: Person { FirstName = John, LastName = Kyrylchuk } Person anotherMe = new() { FirstName = "Oleg", LastName = "Kyrylchuk" };
C onsole.WriteLine(me == anotherMe);
// Output: True record struct Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
} record struct Product(string Name, decimal Price);

Struct 字段支持初始化

using System;

Person person = new() { Name = "Oleg" };

Console.WriteLine(person.Id + " " + person.Name);
// Output: 0cc6caac-d061-4f46-9301-c7cc2a012e47 Oleg struct Person
{
public Guid Id { get; init; } = Guid.NewGuid();
public string Name { get; set; }
}

Lambda 表达式的 Attributes 支持

C# 9 支持本地函数的 Attributes, C# 10 添加了 Lambda 表达式的 Attributes 支持。

Action a = [MyAttribute] () => { };
Action<int> b =[return: MyAttribute] (x) => { };
Action<int> c =[MyAttribute] ([MyAttribute] x) => { }; class MyAttribute : Attribute
{ }

Lambda 中的显式返回类型

Test<int>();

var l1 = string () => string.Empty;
var l2 = int () => 0;
var l3 = static void () => { }; void Test<T>()
{
var l4 = T () => default;
}

应用于方法的 AsyncMethodBuilder 特性

从 C# 7 开始,您只能将AsyncMethodBuilder 特性应用于类型, 在 C# 10 中,您还可以将该特性应用于单个方法。

using System.Runtime.CompilerServices;

class Example
{
[AsyncMethodBuilder(typeof(AsyncVoidMethodBuilder))]
public void ExampleMethod()
{
}
}

结构体中的表达式

C# 10 支持 将 with 表达式和 struct 一起使用

Product potato = new() { Name = "Potato", Category = "Vegetable" };
Console.WriteLine($"{potato.Name} {potato.Category}");
// Output: Potato Vegetable Product tomato = potato with { Name = "Tomato" };
Console.WriteLine($"{tomato.Name} {tomato.Category}");
// Output: Tomato Vegetable struct Product
{
public string Name { get; set; }
public string Category { get; set; }
}

匿名类型中的表达式

C# 10 支持 将 with 表达式和匿名类型一起使用

var potato = new { Name = "Potato", Category = "Vegetable" };
Console.WriteLine($"{potato.Name} {potato.Category}");
// Output: Potato Vegetable var onion = potato with { Name = "Onion" };
Console.WriteLine($"{onion.Name} {onion.Category}");
// Output: Onion Vegetable

13 个 C# 10 特性的更多相关文章

  1. 使用FROM确认按钮(键盘13号键)提交特性并使用ajax.POST提交.

    如果又想使用FROM确认按钮(键盘13号键)提交特性  还能继续用AJAX.POST提交.就需要使用return false 来阻止FROM默认提交 代码如下: HTML页面 这里最关键就是用了ret ...

  2. Ubuntu 13.04/12.10安装Oracle 11gR2图文教程(转)

    Ubuntu 13.04/12.10安装Oracle 11gR2图文教程 原文标题:How to Install Oracle 11G R2 Enterprise Edition Database U ...

  3. MIT 6.828 JOS学习笔记13 Exercise 1.10

    Lab 1 Exercise 10 为了能够更好的了解在x86上的C程序调用过程的细节,我们首先找到在obj/kern/kern.asm中test_backtrace子程序的地址, 设置断点,并且探讨 ...

  4. ML.NET 0.10特性简介

    IDataView被单独作为一个类库包 IDataView组件为表格式数据提供了非常高效的处理方式,尤其是用于机器学习和高级分析应用.它被设计为可以高效地处理高维数据和大型数据集.并且也适合处理属于更 ...

  5. ActiveMQ学习笔记(13)----Destination高级特性(一)

    1. Wildcards 1. Wildcards用来支持名字分层体系,它不是JMS规范的一部分,是ActiveMQ的扩展. ActiveMQ支持一下三种wildcards: 1. ".&q ...

  6. dbt 0.13.0 新添加特性sources 试用

    dbt 0.13 添加了一个新的功能sources 我呢可以用来做以下事情 从基础模型的源表中进行数据选择 测试对于源数据的假设 计算源数据的freshness source 操作 定义source ...

  7. 第13月第10天 swift3.0

    1. Type 'Any' has no subscript members 这一条简直莫名其妙.大体意思就是,你这个类型"Any"不是个数组或者字典,不能按照下标取东西. 我之前 ...

  8. mysql 8.0.13 zip windows 10安装

    1.下载安装包 https://dev.mysql.com/downloads/mysql/ 下载后解压到D:\Program Files\mysql-8.0.13-winx64 2.添加配置文件my ...

  9. spark2.0的10个特性介绍

    1. Spark 2.0 ! 还记得我们的第七篇 Spark 博文里吗?里面我用三点来总结 spark dataframe 的好处: 当时是主要介绍 spark 里的 dataframe,今天是想总结 ...

随机推荐

  1. .Net 下高性能分表分库组件-连接模式原理

    ShardingCore ShardingCore 一款ef-core下高性能.轻量级针对分表分库读写分离的解决方案,具有零依赖.零学习成本.零业务代码入侵. Github Source Code 助 ...

  2. 基于python win32setpixel api 实现计算机图形学相关操作

    最近读研期间上了计算机可视化的课,老师也对计算机图形学的实现布置了相关的作业.虽然我没有系统地学过图形可视化的课,但是我之前逆向过一些游戏引擎,除了保护驱动之外,因为要做透视,接触过一些计算机图形学的 ...

  3. A Child's History of England.34

    'Prince!' said Fitz-Stephen, 'before morning, my fifty and The White Ship shall overtake [超过, 别和take ...

  4. Angular Service设计理念及使用

    官方认为组件不应该直接获取或保存数据, 它们应该聚焦于展示数据,而把数据访问的职责委托给某个服务. 而服务就充当着数据访问,逻辑处理的功能.把组件和服务区分开,以提高模块性和复用性. 1.依赖注入 注 ...

  5. Netty实现Socket

    Netty实现Socket 从Java1.4开始, Java引入了non-blocking IO,简称NIO.NIO与传统socket最大的不同就是引入了Channel和多路复用selector的概念 ...

  6. java中的collection小结

    Collection 来源于Java.util包,是非常实用常用的数据结构!!!!!字面意思就是容器.具体的继承实现关系如下图,先整体有个印象,再依次介绍各个部分的方法,注意事项,以及应用场景.   ...

  7. malloc() vs new

    Following are the differences between malloc() and operator new. (1)new calls constructors, while ma ...

  8. sql优化的8种方式

    1.设置索引. MySQL索引操作:给表列创建索引: 建表时创建索引: create table t(id int,name varchar(20),index idx_name (name)); 给 ...

  9. 探究Go-YCSB做数据库基准测试

    本篇文章开篇会介绍一下Go-YCSB是如何使用,然后按照惯例会分析一下它是如何做基准测试,看看它有什么优缺点. 转载请声明出处哦~,本篇文章发布于luozhiyun的博客: https://www.l ...

  10. 【JAVA今法修真】 第三章 关系非关系 redis法器

    您好,我是南橘,万法仙门的掌门,刚刚从九州世界穿越到地球,因为时空乱流的影响导致我的法力全失,现在不得不通过这个平台向广大修真天才们借去力量.你们的每一个点赞,每一个关注都是让我回到九州世界的助力,兄 ...