zoukankan      html  css  js  c++  java
  • C# – 10.0

    前言

    之前写过 6.0, 7.0, 8.0, 9.0 总结.

    10.0 也是有些好东西哦, 尤其是 pattern matching 的完善, 差不多是时候可以重构 if else switch 的写法了.

    主要参考

    C# 10: New features and examples that are not in C# 9 (.NET 6)

    Every feature added in C# 10 with examples

    The evolution of Pattern Matching in C# (from version 6 to 10) <-- pattern matching 重点看这视频

    Global using

    只要在其中 1 个 .cs file 写 global using, 就等于在所有 .cs file 写了 using

    before, 必须 2 个 file 都写 using

    after, 只要其中一个写就可以了

    虽然我们提倡各个 file 应该要各种负责, 按需 using, 但是把一些常用的放一起也是一种好的管理方式. 

    不错用

    namespace 省略 bracket

    这个有点像 using 省略 bracket 一样

    before 

    after

    注: namespace 结尾要加 semicolon 哦.

    must use 

    Constant + Interpolated String

    before

    会 error

    after

    没 error 了

    const string 和 js 的 const 一样, 就是不能改.

    定义匿名函数返回类型

    var method = string? () => null;

    string? 就是函数的返回类型, 厉害吧.

    解构 + 声明 + 赋值

    var method = () => ("a", 11);
    int number;
    (var value, number) = method();

    这个比 Typescript 还厉害. var 声明 value, 同时赋值 number

    Record 总结

    Intro to Records in C# 9 - How To Use Records And When To Use Them

    Record Structs

    Record Structs

    首先要搞懂 class 和 struct, 看这篇,

    最简单的理解是 class 是引用类型, struct 是值类型

    它们都是 key-value 结构.

    9.0 开始 C# 多了一个 record, 它也是 key-value 结构, 所以又多一个傻傻分不清楚了.

    依据上面的视频, record 是引用类型, 但是它又是 immutable 的 (内容不可以改)

    熟悉函数式的人应该对 immuatable 的好处有共鸣啦. 要不你用过 React, Angular 也应该听过它.

    简单理解就是它把 class 改装成了一种不可以改的对象.

    由于它们太像了, 10.0 干脆做了个 mix record class, record struct, Oh Yeah 更傻傻分不清楚了.

    总之记得: 

    class, record 是引用类型, struct 是值类型

    record, record class, record struct 都支持 with 语法, 就是可以 clone 赋值.

    class 通过 property set init, Deconstruct 也可以做到类似 record 的感觉. 但它不能用 with 语法来 clone 赋值, record class 才可以用 with.

    record class 如果没有做 set init 它是可以改值的哦. 它只是让 class 可以 with 而已.

    最后我目前依然没有用到 record 或者 immuatable 概念, 所以以后用到了才分享多一点.

    record 长这样

    定义

    public record Teacher (string Name);

    创建 + 赋值

    var teacher1 = new Teacher(Name: "Derrick");
    var teacher2 = teacher1 with { Name = "new name" };

    Pattern Matching 总结

    Pattern matching 是一种条件判断的表达方式. 它的目的是提升可读性, 减少没必要的重复语句.

    凡是用到条件判断的地方都可以用. 比如 if else, switch, 甚至 catch

    它的关键就是对 is 提升了很多, 较早的版本, is 只能用来判断类型而已. 现在是整个 pattern matching 的开头关键字丫.

    is null, is not null

    var person = new Person();
    if (person is Person) { }; // 5.0
    if (person is null) { }; // 7.0
    if (person is not null) { }; // 9.0

    一路走来也是不容易啊, 当 9.0 才有 is not null.

    is 当 ==, >, >=, <, <= 用

    var name = "dada";
    var age = 1;
    if (name is "dada") { }
    if (age is >= 10) { }

     

    and 和 or

    var name = "dada";
    var age = 1;
    if (name is "dada" or "tata") { }
    if (age >= 10 && age <= 100) { } // before
    if (age is >= 10 and <= 100) { } // after

    before 需要重复 age, after 少了重复, 也把符号变成语句, 这样就更好读了.

    is 配上 and

    var value = "abc";
    if (value is not "abc" and not "xyz")
    { 
            
    }

    如果想判断 value "不是 abc" 同时 "不是 xyz" 写法是上面这样. 2 个 not, 而不是 

    value is not "abc" and "xyz", 这句的意思是 value 是 "不是 abc" 同时 "是 xyz"

    对属性进行判断

    if (animal is { Name: "dada" or "tata", Age: > 10 or < 18 }) { }

    再加一个类型判断也可以

    if (animal is Dog { Name: "dada" or "tata", Age: > 10 or < 18 }) { }

    子属下也可以直接 dot 出来

    if (animal is Dog { child.ChildProp: "dada" or "tata" }) { }

     

    双值判断

    if ((name, age) is ("dada" or "tata", > 10 and < 20)) { }

    skip 掉第一个判断 (switch 的时候比较会用到)

    if ((name, age) is (_, > 10 and < 20)) { }

    用到 switch case 上

    上面的判断也适用于 switch case

    switch case return

    var result = animal switch
    {
        { Name: "dada" or "tata", Age: > 10 and < 100 } => "value1",
        _ => "default"
    };

    switch case action

    switch (animal)
    {
        case { Name: "dada" or "tata", Age: > 10 and < 20 }:
            // do somthing
            break;
        default:
            // defualt
            break;
    };

    switch case action 爽值判断

    switch (name, age)
    { 
        case ("dada" or "tata", > 10 and < 20): 
            // do something
        break;
        default:
            // defualt
            break;
    }

    Pattern matching 目前已经比较成熟了, 但我相信它还有不足够的地方,而且也不是 100% 场景都合适. 

    但我觉得, 已经是时候可以开始尝试了.

    Linq 提升

    虽然这个不算 C# 语法, 但是我也没有地方写了, 干脆记入这里吧.

    参考: 

    Bite-Size .NET 6 - UnionBy, IntersectBy, ExceptBy, and DistinctBy

    Bite-Size .NET 6 - MaxBy() and MinBy() in LINQ

    大同小异的功能, 我解释一个就好了呗.

    IntersectBy

    var dtoProperties = dto.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
    var entityProperties = entity.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();

    2 个 list, 我要找出相同 property.name 的

    var intersectProperties1 = dtoProperties.Intersect(intersectProperties, comparer);

    从前需要写一个 comparer. 超级不友好.

    有时宁愿牺牲表达, 用 Where 来替代.

    var intersectProperties = dtoProperties.Where(dtoProperty => entityProperties.Any(entityProperty => dtoProperty.Name == entityProperty.Name)).ToList();

    现在不用烦, IntersectBy 搞定. 

    var intersectProperties2 = dtoProperties.IntersectBy(entityProperties.Select(p => p.Name), p => p.Name).ToList();
  • 相关阅读:
    Mysql 主主复制失败恢复【转】
    linux的curl用法【转】
    做Mysql主从时,注意使用replicate_wild_do_table和replicate-wild-ignore-table【转】
    MyEclipse 2014配置Maven
    使用MyEclipse 2014构建Maven项目的两种方法
    Struts2的输入校验
    Java Web之Filter
    Struts2国际化
    Struts2实现登录流程
    Java实现文件MD5加密
  • 原文地址:https://www.cnblogs.com/keatkeat/p/15432286.html
Copyright © 2011-2022 走看看