C#9的特征

目录

使用所有东西

在你破坏分配之前进行自检

Null检查不再Kotlinified

让构造函数去做

如果这还不够简单,那么记录呢?

但是,等等-还有更多!


作为我的一种语言,我一直对C#情有独钟。它是我在大学里学习的第一批高级语言之一,并且在十年的大部分时间里,它已经成为我日常职业生活的一部分。不断创新的概念是该语言始终令人愉悦的事情之一。

功能不断地从其他语言(适应您的F#,KotlinTypescript等)进行改编,以帮助添加非常需要的功能,扩展现有功能或仅使用大量语法糖。我认为新的一年才刚刚开始,让我们来看看一些建议的特征,这些特征将在不久的将来成为微软的旗舰语言!

值得注意的是,这绝不是一个完整的列表,并且该列表中的所有内容(与活动开发中的所有内容一样)都可能发生更改/永远不会发生。

所以,让我们开始吧!

使用所有东西

在下一个C#版本中拥护的提案之一是扩展using语句以改善资源管理的概念目前,一条using语句可能需要一些仪式,但是该提议将增加对以声明方式使用using语句的支持:

if (condition)  
{
     using var file1 = new FileStream("...");
     using var file2 = new FileStream("...");

     // Omitted for brevity

     // Dispose file2
     // Dispose file1
}

using的作用域结束时,此方法将为所有用该语句修饰的变量释放资源。提案的另一个方面是使用模式匹配来识别给定类上是否存在Dispose()方法的概念,当作用域结束时,将类似于上述示例:

switch(...)  
   case someCase:
       using var connection = new SqlConnection();

       // Omitted for brevity

       // Dispose connection (since SqlConnection has a Dispose() method)
       break;

支持此功能不需要目标类实现IDisposible接口,而只需包含Dispose()方法。

在你破坏分配之前进行自检

在应用程序中通常会看到的一种常见模式是在某些赋值语句之前使用简单的“is null检查,如下所示:

if (something == null)  
{
    something = someValue;
}

此建议建议引入一个新的二进制运算符,其目的是简化此语法,类似于使用null-coalescing运算符进行的改进:

// The ??= operator is functionally equivalent to a null check followed 
// by an assignment statement (if null)
something ??= someValue;

这是一种悬而未决的果实,但是它为常见的模式提供了一些语法糖,您肯定会在许多应用程序中看到这种模式。

Null检查不再Kotlinified

没有人真正喜欢写无数null检查。if语句的行与嵌套异常一起抛出,这很麻烦。与以前的功能类似,该建议旨在通过允许将运算符!添加到参数中以向编译器指示给定值不会null ,从而消除了对显式null检查的需要

它将转换如下代码段:

int CountWords(string sentence)  
{
    if (sentence is null) 
    {
        throw new ArgumentNullException(nameof(sentence));
    }

    // Omitted for brevity
}

转换成更简洁、整洁的形式:

// Notice the trailing '!' after the parameter name, which indicates
// it will not be null
int CountWords(string sentence!)  
{
    // Omitted for brevity
}

让构造函数去做

C6以来一直在讨论中或处于讨论阶段的另一个建议是主构造函数的想法,该特性已在TypescriptKotlin等语言中找到。基本构造函数的基本思想是,它们通常通过根据构造函数本身传递的参数隐式创建私有字段,从而简化编写类构造函数和样板代码的过程。

让我们看一个具有几个privatereadonly属性的类的示例:

public class Widget  
{
    private readonly int _foo;
    private readonly WidgetConfiguration _config;

    public Widget(int foo, WidgetConfiguration config)
    {
         _foo = foo;
         _config = config;
    }
}

该提案将消除对样板字段声明的需求,因为构造函数本身将处理创建传入的参数:

public class Widget  
{
     public Widget(int _foo, WidgetConfiguration _config)
     {
          // If you wanted one of these properties to be publicly accessible, you could define
          // and set one of those here, otherwise the arguments will be privately accessible
          // as fields.
     }
}

如果这还不够简单,那么记录呢?

如果您喜欢上一个建议的功能,那么您可能会发现自己对此功能有双重看法。一段时间以来,另一个提议是记录的概念。记录是用于声明类和结构的简化形式,该类和结构支持一些新功能以简化使用它们的过程(例如,调用方——接收方参数和表达式)。

public class Widget  
{
    // Properties (business as usual)
    public readonly string Foo;
    public readonly string Bar;

    // Definition of a With expression, which will allow you to 
    // easily create instances of a widget
    // from an existing instance
    public Widget With(string foo = this.Foo, string bar = this.Bar) => new Widget(foo, bar);
}

我们可以看到以下示例:

var existingWidget = new Widget("foo", "bar");

// Now create a brand new instance with one of the properties changed
var clonedWidget = existingWidget.With(bar: "buzz");

// At this point clonedWidget looks like this: { Foo = "foo", Bar = "buzz" }

记录还将支持使用位置模式匹配以及同时使用销毁来执行以下操作

var widget = new Widget("foo", "bar");

// If the widget has a its Foo property set to "foo", then this condition will be met
if (widget is Widget("foo", var bar)) {  
    // Perform some operation here on the widget, 
    // the use of var above will function as destruction so you can
    // access it within this scope
    Console.WriteLine(bar); 
}

记录是C9中提出的最复杂的功能之一,因此,如果您好奇或想了解更多有关记录的信息,我强烈建议在此处查看有关记录的完整建议,其中包含大量的示例、用例等。

但是,等等-还有更多!

这篇文章中介绍的特征是一些日常可能会看到的更实用的特征,但这绝不是当前的所有提议。如果您有兴趣进一步了解GitHubC9的相关里程碑菜单上的内容,我将鼓励您

在这里,您会发现大量此处未提及的其他特征,例如:

最后——C#语言是开源的,并且一直在寻找贡献者,见解以及任何热爱该语言的人分享他们对如何使它变得更好的想法。

发布了70 篇原创文章 · 获赞 130 · 访问量 42万+

猜你喜欢

转载自blog.csdn.net/mzl87/article/details/104010842
C9
今日推荐