惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
Martin Fowler
Martin Fowler
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Last Watchdog
The Last Watchdog
S
Schneier on Security
C
Cisco Blogs
P
Privacy International News Feed
T
Tenable Blog
Spread Privacy
Spread Privacy
Recent Commits to openclaw:main
Recent Commits to openclaw:main
N
News and Events Feed by Topic
Security Archives - TechRepublic
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
Project Zero
Project Zero
GbyAI
GbyAI
N
Netflix TechBlog - Medium
T
Tor Project blog
雷峰网
雷峰网
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Threat Research - Cisco Blogs
Cyberwarzone
Cyberwarzone
L
LangChain Blog
MyScale Blog
MyScale Blog
C
CERT Recently Published Vulnerability Notes
C
Check Point Blog
G
Google Developers Blog
T
Tailwind CSS Blog
L
LINUX DO - 热门话题
宝玉的分享
宝玉的分享
IT之家
IT之家
F
Fortinet All Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Announcements
Recent Announcements
T
The Exploit Database - CXSecurity.com
Hacker News: Ask HN
Hacker News: Ask HN
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Google Online Security Blog
Google Online Security Blog
Help Net Security
Help Net Security
H
Hacker News: Front Page
小众软件
小众软件
U
Unit 42
Apple Machine Learning Research
Apple Machine Learning Research
P
Privacy & Cybersecurity Law Blog
T
Threatpost

博客园 - Rwing

C# 13(.Net 9) 中的新特性 - 半自动属性 [翻译].NET 8 的原生AOT及高性能Web开发中的应用[附性能测试结果] C# 13(.Net 9) 中的新特性 - 扩展类型 如何在 Docker 环境下自动给 .NET 程序生成 Dump [翻译]正式宣布 Visual Studio 2022 [翻译]Azure 网关迁移至 .NET Core 3.1 性能提升一倍 C# 9.0 新特性预览 - init-only 属性 C# 9.0 新特性预览 - 顶级语句 C# 9.0 新特性预览 - 空参数校验 C# 9.0 新特性预览 - 类型推导的 new - Rwing [翻译] 使用 Serverless 和 .NET Core 构建飞速发展的架构 - Rwing [翻译] .NET Core 3.0 RC 1 发布 - Rwing [翻译] .NET Core 3.0 Preview 9 发布 - Rwing [翻译] .NET Core 3.0 Preview 7 发布 - Rwing [翻译] C# 8.0 接口默认实现 - Rwing [翻译] 正式宣布 .NET 5 - Rwing [翻译] ASP.NET Core 利用 Docker、ElasticSearch、Kibana 来记录日志 - Rwing [翻译] Visual Studio 2019: 极速编码. 智能工作. 创造未来. - Rwing [翻译] 使用 .NET Core 3.0 创建一个 Windows 服务 - Rwing
[翻译] 预览 C# 10 的新东西
Rwing · 2021-06-01 · via 博客园 - Rwing

原文: [Introducing C# 10]
作者: Ken Bonny

本周早些时候(译注:原文发表于5月1日),我关注了 Mads TorgersenDotNet SouthWest 大会上的演讲,他是微软的 C# 语言的首席设计师。他概述了 C# 10 即将包含的很酷的一些新东西。让我们来快速浏览一下。

小小的免责声明,这些变化中的大部分已经基本完成。但是由于它仍在积极的开发中,我不能保证 C# 10 发布时所有东西都会完全如实。

struct record

他谈到的第一件事是,目前 record 的实现是使用一个 class 作为基础对象的。现在还会有一个 record struct 的变体可用,所以基础类型可以是一个值类型。区别在于,普通的 record 在函数之间传递的是引用,而 record struct 是其值的拷贝。 record struct 也会支持 with 运算符。

同时,还允许向 record添加运算符,两种 record 类型都可以。

record Person(string Name, string Email)
{
  public static Person operator +(Person first, Person second)
  {
    // logic goes here
  }
}

required

C# 团队关注的目标之一是使对象的初始化变得更容易。这就是为什么可以对 class、struct、record 或 struct record 的属性添加 required 标记 。它使得这些属性必须填写。这可以通过构造函数来完成,也可以通过对象初始化来完成。下面的两个类的定义是等价的。如果你添加了 required 关键字,那么就无法在不设置 Name 属性的情况下将Person 实例化。编译器会抛出错误,无法编译。

class Person
{
  public required string Name { get; set; }
  public DateTime DateOfBirth { get; set; }
}
class Person
{
  public Person(string name) => Name = name;

  public string Name { get; set; }
  public DateTime DateOfBirth { get; set; }
}

field

为了进一步的改善属性,将允许完全摆脱 backing field 。新的关键字 field 将提供对上述字段的访问。对setter 和 init only 属性都可以使用。

class Person
{
  public string Name { get; init => field = value.Trim(); }
  public DateTime DateOfBirth { get; set => field = value.Date; }
}

with

在下一个版本中还会有一些有趣的小改进。其实中一个是匿名类型也将支持 with 运算符。

var foo = new
{
  Name = "Foo",
  Email = "foo@mail.com"
};
var bar = foo with {Name = "Bar"};

namespace

现在可以创建一个带有命名空间导入的文件,然后在任何地方都可以使用这个导入。例如,如果有一个很常用的命名空间,几乎在每个文件中都使用例如 Microsoft.Extensions.Logging.ILogger ,那么就可以在任何.cs文件(我建议在 Program.cs 或专门的 Imports.cs )中添加一行 global using Microsoft.Extensions.Logging.ILogger,之后这个命名空间将可以在整个项目中使用。注意,这不适用于整个解决方案! 没有人能够预测哪些地方需要导入,所以它们被分组到每个项目中。

// 译注:原文并没有提供代码示例,为了更好方便大家理解私自添加了一个演示
// Program.cs 文件
global using System;

// Sample.cs 文件
// 可以不用再using System;
Console.WriteLine("foo");

随后,还将对命名空间也会有一个优化。现在命名空间需要大括号 {} 来包起来代码,这就意味着所有代码至少要缩进一次。为了节省 tab(或四个空格)和屏幕空间,在文件的任何地方添加一个命名空间,将使所有代码都属于该命名空间。有研究表明绝大多数情况下,一个文件中所有的代码都属于同一个命名空间。使用这个方案后,文件大小随之减少,这对一个解决方案来说可能并不明显(即使它包含成千上万的文件),但在GitHub/GitLab/BitBucket/... 的规模上,我认为这将为他们节省一些空间。如果有人仍想在一个文件中包含多个命名空间,使用大括号的选项仍然可用。

// 传统的方式 LegacyNamespace.cs
namespace LegacyNamespace
{
  class Foo
  {
    // legacy code goes here
  }
}

// 简化的方式 SimplifiedNamespace.cs
namespace SimplifiedNamespace;
class Bar
{
  // awesome code goes here
}

lambda

对 lambda 语句也会有一些很酷的更新。编译器将对推断 lambda 签名提供更好的支持,而且还可以添加特性。话可以显式指定返回类型,以帮助编译器理解 lambda。

var f = Console.WriteLine;
var f = x => x; // 推断返回类型
var f = (string x) => x; // 推断签名
var f = [NotNull] x => x; // 添加特性
var f = [NotNull] (int x) => x;
var f = [return: NotNull] static x => x; // 添加特性
var f = T () => default; // 显式返回类型
var f = ref int (ref int x) => ref x; // 使用 ref 
var f = int (x) => x; // 显式指定隐式输入的返回类型
var f = static void (_) => Console.Write("Help");

感谢 Schooley 提出了一个不那么容易混淆的特性例子

interface

最后,将有可能在接口上指定静态方法和属性。我知道这将是一个有争议的话题,就像给接口添加默认实现一样。我不喜欢它。然而,这可能非常有趣。想象一下,你可以指定一个接口的默认值或指定创建方法。

interface IFoo
{
  static IFoo Empty { get; }
  static operator +(IFoo first, IFoo second);
}
class Foo : IFoo
{
  public static IFoo Empty => new Foo();
  public static operator +(IFoo first, IFoo second) => /* do calculation here */;
}

就个人而言,我喜欢这些变化。我最喜欢的是对命名空间的改变和对接口的改进。总之,未来是光明的 C# 的。嗯嗯... (译注:这里作者玩了一个梗,原文 the future is seeing sharp,see sharp发音类似 C# )
谢谢各位,大家再见。