C# 9.0 新功能一览!



new Person{FirstName = "Scott",LastName = "Hunter"}
public class Person{public string FirstName { get; set; }public string LastName { get; set; }}
public class Person{public string FirstName { get; init; }public string LastName { get; init; }}

public class Person{private readonly string firstName;private readonly string lastName;public string FirstName{get => firstName;init => firstName = (value ?? throw new ArgumentNullException(nameof(FirstName)));}public string LastName{get => lastName;init => lastName = (value ?? throw new ArgumentNullException(nameof(LastName)));}}

public data class Person{public string FirstName { get; init; }public string LastName { get; init; }}

var otherPerson = person with { LastName = "Hanselman" };protected Person(Person original) { /* copy all the fields */ } // generated
var originalPerson = otherPerson with { LastName = "Hunter" };
public data class Person { string FirstName; string LastName; }public data class Person{public string FirstName { get; init; }public string LastName { get; init; }}
private string firstName;
public data class Person{string FirstName;string LastName;public Person(string firstName, string lastName)=> (FirstName, LastName) = (firstName, lastName);public void Deconstruct(out string firstName, out string lastName)=> (firstName, lastName) = (FirstName, LastName);}
public data class Person(string FirstName, string LastName);var person = new Person("Scott", "Hunter"); // positional constructionvar (f, l) = person; // positional deconstruction


public data class Person { string FirstName; string LastName; }public data class Student : Person { int ID; }
Person person = new Student { FirstName = "Scott", LastName = "Hunter", ID = GetNewId() };otherPerson = person with { LastName = "Hanselman" };

Person person1 = new Person { FirstName = "Scott", LastName = "Hunter" };Person person2 = new Student { FirstName = "Scott", LastName = "Hunter", ID = GetNewId() };

using System;class Program{static void Main(){Console.WriteLine("Hello World!");}}
using System;Console.WriteLine("Hello World!");

public static decimal CalculateToll(object vehicle) =>vehicle switch{...DeliveryTruck t when t.GrossWeightClass > 5000 => 10.00m + 5.00m,DeliveryTruck t when t.GrossWeightClass < 3000 => 10.00m - 2.00m,DeliveryTruck _ => 10.00m,_ => throw new ArgumentException("Not a known vehicle type", nameof(vehicle))};

DeliveryTruck => 10.00m,
DeliveryTruck t when t.GrossWeightClass switch{> 5000 => 10.00m + 5.00m,< 3000 => 10.00m - 2.00m,_ => 10.00m,},

DeliveryTruck t when t.GrossWeightClass switch{< 3000 => 10.00m - 2.00m,>= 3000 and <= 5000 => 10.00m,> 5000 => 10.00m + 5.00m,},
not null => throw new ArgumentException($"Not a known vehicle type: {vehicle}", nameof(vehicle)),null => throw new ArgumentNullException(nameof(vehicle))
if (!(e is Customer)) { ... }if (e is not Customer) { ... }

Point p = new (3, 5);
Person person = student ?? customer; // Shared base typeint? result = b ? 0 : null; // nullable value type

abstract class Animal{public abstract Food GetFood();...}class Tiger : Animal{public override Meat GetFood() => ...;}


更多精彩推荐
你点的每个“在看”,我都认真当成了喜欢
关注公众号:拾黑(shiheibook)了解更多
[广告]赞助链接:
四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/
关注网络尖刀微信公众号随时掌握互联网精彩
赞助链接
排名
热点
搜索指数
- 1 习近平将发表二〇二六年新年贺词 7904141
- 2 2026年国补政策来了 7808738
- 3 东部战区:开火!开火!全部命中! 7712893
- 4 2026年这些民生政策将惠及百姓 7616985
- 5 小学食堂米线过期2.5小时被罚5万 7519709
- 6 解放军喊话驱离台军 原声曝光 7428214
- 7 为博流量直播踩烈士陵墓?绝不姑息 7327605
- 8 每月最高800元!多地发放养老消费券 7238391
- 9 数字人民币升级 1月1日起将计付利息 7141831
- 10 2026年1月1日起 一批新规将施行 7040675









CSDN
