C# 10 讲解一

介绍

接下来我将给大家重点介绍一下.Net 6 之后的一些新的变更,文章都是来自于外国大佬的文章,我这边进行一个翻译,并加上一些自己的理解和解释。

源作者链接:https://anthonygiretti.com/

正文

全局引用

首先先我想和大家分享一下我最喜欢的使用Global usings的方式。我喜欢处理包含所有 usings 的单个文件。

然后从 C# 文件中删除所有using引用,然后创建一个GlobalUsings.cs,加入下面代码来使用

global using System;
global using Microsoft.EntityFrameworkCore;
global using Microsoft.EntityFrameworkCore.Migrations;
global using Microsoft.EntityFrameworkCore.Infrastructure;
global using Microsoft.EntityFrameworkCore.Metadata;
global using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
global using Easy.Core.Flow.EFTemporal;

如果您使用多个项目,则必须在每个项目上声明您的全局使用。

解构

解构对象允许您使用最简单的语法将其属性分配给变量。

为此,您显然必须实现一个解构器。

让我们以包含FirstName和LastName属性的Person类为例,解构器(public void Deconstruct)将如下所示:

public class Person
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public void Deconstruct(out string firstName, out string lastName) =>
        (firstName, lastName) = (this.FirstName, this.LastName);
}
public class Program
{
    public static void Main(string[] args)
    {
        var person = new Person
        {
            FirstName = "Anthony",
            LastName = "Giretti"
        };

        // Declaration and assignation syntax:
        (string firstname1, string lastname1) = person;

        // OR

        // Declaration, initialization and assignation syntax:
        string firstname2 = string.Empty;
        string lastname2 = string.Empty;
        (firstname2, lastname2) = person;
    }
}
public class Program
{
    public static void Main(string[] args)
    {
        var person = new Person
        {
            FirstName = "Anthony",
            LastName = "Giretti"
        };

        // Mixing declaration, initialization and assignation with declaration and assignation syntaxes:
        // Before C# 10: Error CS8184 A deconstruction cannot mix declarations and expressions on the left-hand-side.	
        string lastname = string.Empty;
        (string firstname, lastname) = person;
    }
}

扩展属性模式

C#8的语法示例

namespace DemoCsharp9
{
    public class ProvinceOrStateTaxProperty
    {
        public string ProvinceOrState { get; set; }
    }

    public class CalculateTax
    {
        public string CountryName { get; set; }
        public ProvinceOrStateTaxProperty ProvinceTaxProperty { get; set; }
    }

    public class Computing
    {
        private static decimal ComputePrice(CalculateTax calculate, decimal price) =>

        calculate switch
        {

            { ProvinceTaxProperty: { ProvinceOrState: "Québec" } } => 1.14975M * price,

            { ProvinceTaxProperty: { ProvinceOrState: "Alberta" } } => 1.05M * price,

            { ProvinceTaxProperty: { ProvinceOrState: "Ontario" } } => 1.13M * price,

            _ => 0

        };
    }
}

早期我们访问嵌套属性的语法有点冗长:{ ProvinceTaxProperty:{ ProvinceOrState:“价值”}}

C#10 让语法更加清晰,我们可以直接访问嵌套属性

namespace DemoCsharp10
{
    public class ProvinceOrStateTax
    {
        public string ProvinceOrState { get; set; }
    }

    public class CountryTax
    {
        public string CountryName { get; set; }
        public ProvinceOrStateTax ProvinceTaxProperty { get; set; }
    }

    public class Computing
    {
        private static decimal ComputePrice(CountryTax calculate, decimal price) =>

        calculate switch
        {

            { ProvinceTaxProperty.ProvinceOrState: "Québec" } => 1.14975M * price,

            { ProvinceTaxProperty.ProvinceOrState: "Alberta" } => 1.05M * price,

            { ProvinceTaxProperty.ProvinceOrState: "Ontario" } => 1.13M * price,

            _ => 0

        };
    }
}

结语

联系作者:加群:867095512 @MrChuJiu

公众号

posted @ 2022-01-24 22:30  初久的私房菜  阅读(586)  评论(0编辑  收藏  举报
作者:初久的私房菜
好好学习,天天向上
返回顶部小火箭
好友榜:
如果愿意,把你的博客地址放这里
张弛:https://blog.zhangchi.fun/