美文网首页
盘点C# 9.0中好用的特性

盘点C# 9.0中好用的特性

作者: 程序员ken | 来源:发表于2023-04-21 16:32 被阅读0次

顶级语句

将类和类里面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 _) { return 0; }
public delegate void FuncA(string a, string c);

        static void Main(string[] args)
        {

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

        }

相关文章

  • C#语言特性发展史

    C#语言特性发展史 Intro 本文主要总结介绍C# 每个版本带来的不同的语言特性。 C#,读作C Sharp,是...

  • C#特性(Attribute)-现学现用

    前言 想要灵性的使用C#反射机制,特性(Attribute)的使用是必不可少的。 C# 特性(Attribute)...

  • C#枚举类型常用扩展方法(三)

    C#获取枚举值特性(Display、Description、自定义特性) 一、Display特性 调用如下: 二、...

  • 目录 - C#

    总目录 C# 第01局:泛型 C# 第02局:反射 C# 第03局:特性 C# 第04局:委托 C# 第05局:事...

  • C# 语言历史版本特性(C# 1.0到C# 8.0汇总)

    C# 1.0 特性Classes:面向对象特性,支持类类型Structs:结构Interfaces:接口Event...

  • C# 特性

    泛型 无 lambda表达式 无 匿名函数 无 委托 winfrom界面委托 在对控件进行操作的时候,可以通过判断...

  • c#特性

    c#特性: 特性(Attribute)是用于在运行时传递程序中的各种元素(比如类,方法,结构,枚举,组件等)的行为...

  • Unity 之如何写出强壮的代码

    【反射】 Unity C#基础之 反射反射,程序员的快乐 Unity C#基础之 特性,一个灵活的小工具 【多线程...

  • c#中的特性

    c#中的特性 特性在我的理解就是在类或者方法或者参数上加上指定的标记,然后实现指定的效果。 和Java中的注解@A...

  • 异步编程(二):async/await模式之基本操作

    在C#语言中,Async/AWait特性可以创建并使用异步方法。该特性由三个部分组成: 调用方法(calling ...

网友评论

      本文标题:盘点C# 9.0中好用的特性

      本文链接:https://www.haomeiwen.com/subject/ctrjjdtx.html