首页 > 编程语言 >盘点C# 9.0中好用的特性

盘点C# 9.0中好用的特性

时间:2023-04-22 20:44:23浏览次数:37  
标签:string record C# int Student 中好 9.0 public Name

顶级语句

将类和类里面Main函数省略,只留下核心的逻辑代码就是顶级语句!

1.顶级语句1

await System.Threading.Tasks.Task.Delay(1000);
System.Console.WriteLine("Hi!");
return 0;
static class $Program
{
    static async Task<int> $Main(string[] args)
    {
        await System.Threading.Tasks.Task.Delay(1000);
        System.Console.WriteLine("Hi!");
        return 0;
    }
}

1.顶级语句2

System.Console.WriteLine("Hi!");
return 2;
static class $Program
{
    static int $Main(string[] args)
    {
        System.Console.WriteLine("Hi!");
        return 2;
    }
}

Init

struct Point
{
    public int X { get; }
    public int Y { get; }

    public Point(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }
}

init通过允许调用方在构造操作过程中改变成员,访问器使不可变对象更具灵活性。 这意味着对象的不可变属性可以参与对象初始值设定项,因此不再需要类型中的所有构造函数样板。 Point类型现在只是:

struct Point
{
    public int X { get; init; }
    public int Y { get; init; }
}

然后,使用者可以使用对象初始值设定项来创建对象。

var p = new Point() { X = 42, Y = 13 };

记录

记录类型是引用类型,类似于类声明。 如果不包含,记录将提供一个错误 record_base argument_list record_declaration parameter_list 。 部分记录最多只能有一个分部类型声明提供 parameter_list 。
记录参数不能 ref 使用 out 或 this 修饰符 (但 in params 允许) 。

继承

除非类为 object ,且类不能从记录继承,否则记录不能从类继承。 记录可以继承自其他记录。

 public record Student(string name,int age) {
 }
using System;
using System.Collections.Generic;

public class Student : IEquatable<Student>
    {
        #region 属性这部分 可以看到set属性没有
        #记录具有不可变性,记录一旦初始化完成,那么它的属性值将不可修改(可以通过反射修改)
        public string Name { get; init; }
        public int Age { get; init; }
        #endregion

        #region
        protected virtual Type EqualityContract => typeof(Student);
        #endregion
      
        public override bool Equals(object? obj) => Equals(obj as R1);
        public virtual bool Equals(Student? other)
        {
            return !(other is null) &&
                EqualityContract == other.EqualityContract &&
                
                EqualityComparer<string>.Default.Equals(this.Name, other.Name) &&
                EqualityComparer<int>.Default.Equals(this.Age, other.Age);
        }
        public static bool operator ==(R1? left, R1? right)
            => (object)left == right || (left?.Equals(right) ?? false);
        public static bool operator !=(R1? left, R1? right)
            => !(left == right);

         public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("Student");
            builder.Append(" { ");
            if (this.PrintMembers(builder))
            {
                builder.Append(" ");
            }
            builder.Append("}");
            return builder.ToString();
        }
    }

支持解构

public record Student(string Name, int Age) {
        public string Name { get; set; }
        public int Age { get; set; }
}
Student record = new Student("张三",19) ;
var (name, age) = record;

==》这个可以用在switch匹配上 还记得我们
string value = record switch{
                ("张三", 19) => "01",
                ("李四", 25) => "02",
                _ => "default"
 };

## 之所以可以用这个这么用 是因为编译后多了这样的解构函数
 public void Deconstruct(out string Name, out int Age) => (Name, Age) = (this.Name, this.Age);

with 表达式

with表达式是使用以下语法的新表达式。

Student record2  = record with { Name = "王五" };

//在编译后等价于
var temp = record.<Clone>$();
temp.Name = "王五";
record2 = temp;

//不赋值 完全克隆
Student record3  = record with { };

好处:

1.比较两个属性是否相等跟属性设置的顺序无关
2.方便快速克隆,不影响原数据
3.补充了结构体不支持继承以及性能不高的短处

Lambda 弃元参数

允许丢弃 (用作 _ lambda 和匿名方法的参数) 。 例如:

  • lambda: (_, _) => 0 , (int _, int _) => 0
  • 匿名方法: delegate(int _, int _)
public delegate void FuncA(string a, string c);

        static void Main(string[] args)
        {

            FuncA func = (_, _) => { };

        }

标签:string,record,C#,int,Student,中好,9.0,public,Name
From: https://www.cnblogs.com/kenower/p/17343885.html

相关文章

  • 图像边缘检测(Canny)
    Canny检测的流程Canny检测主要是用于边缘检测1)使用高斯滤波器,以平滑图像,滤除噪声。 2)计算图像中每个像素点的梯度强度和方向。3)应用非极大值(Non-MaximumSuppression)抑制,以消除边缘检测带来的杂散响应4)应用双阈值(Double-Threshold)检测来确定真实的和潜在的边缘5)......
  • CS61A_hw09
     defmutate_reverse(link):"""MutatestheLinksothatitselementsarereversed.>>>link=Link(1)>>>mutate_reverse(link)>>>linkLink(1)>>>link=Link(1,Link(2,Link(3))......
  • DASCTF-Crypto-Sign1n
    sign1n显然能导出下面的式子:\(k\timesphi=e^3\times(WHATF-3)-1\).注意到\(phi=(p-1)\times(q-1)=n-(p+q)+1\),\(n=p\timesq\).考虑构造一元二次方程解出pq.同时考虑到\(k\times(p+q-1)<n\),代入得到:\(k\times(p+q-1)=(1-e^3\times(WHATF-3))\modn\).......
  • 深度学习入门系列之doc
    这周老师让把深度学习的名词过一遍,小玛同学准备在过一遍DeepLearning名词的同时把基本的模型也过一遍。感谢杰哥发我深度学习入门系列能让我有机会快速入门。下面就来doc一些学到的东西感知器(线性单元)有个问题就是当面对的数据集不是线性可分的时候,“感知器规则”可能无法收敛......
  • 马拉车(manacher) & 回文自动机(PAM)
    读了徐安矣2023年集训队论文写的,对于差分性质和习题,我会在理解清楚之后再补充。本篇博客仅讨论前两种算法。首先,马拉车和回文自动机都是处理回文串问题的。但在此之前,学习一些更加简单的回文算法。小trick:把给定串的两头和缝隙插入相同字符,且在边界处用不同字符标记,使得长度为......
  • 为什么重写equals方法就一定要重写hashCode方法
    在hashMap和hashTable集合中,元素是不能够重复的,所以我们在添加元素时,先要判断是否存在这个元素。而判断的方法就是先用hashCode方法判断哈希值是否相同,如果哈希值相同,再使用equals判断是否相同,如果都相同,则才证明两个元素不同。而如果哈希值不同,则不会进行后续的equals判断。哈希......
  • CF ER147 div.2
    A 简单计数题,判断前导零。#include<bits/stdc++.h>usingnamespacestd;intT;intmain(){ cin>>T; while(T--){ chars[10]; cin>>s; intn=strlen(s); intans=1; for(inti=0;i<n;i++){ if(s[i]=='?'&&a......
  • 【Visual Leak Detector】源码文件概览
    说明使用VLD内存泄漏检测工具辅助开发时整理的学习笔记。本篇对VLD源码包中的各文件用途做个概述。同系列文章目录可见《内存泄漏检测工具》目录目录说明1.整体概览2.文件夹.teamcity3文件夹lib3.1文件夹cppformat(生成libformat)3.2文件夹dbghelp3.3文件夹gtest......
  • java maven pom指定main class类
    pom文件中增加 <build><finalName>entrance</finalName><!--这里是生成的jar包名字--><plugins><plugin><groupId>org.apache.maven.plugins</groupId><arti......
  • FastCopy v5.0.5 绿色汉化版
    更新流水:2023.04.22:自改官方5.0.5最新正式版本2022.04.20:首个自改官方 5.0.4最新正式版本修改内容:by.th_sjy&安心爱&萌面蛋饺基于th_sjy汉化版为模板,重新修正中文语言(感谢安心爱对日语翻译提供帮助)删除多余语言,解除在WinPE环境下运行限制剔除检测更新及帮助内多余菜单......