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 习近平同马克龙交流互动的经典瞬间 7904132
- 2 公考枪手替考89次敛财千万 7808326
- 3 15岁高中生捐赠南京大屠杀日军罪证 7713313
- 4 2025你的消费习惯“更新”了吗 7617452
- 5 危险信号!俄数百辆保时捷突然被锁死 7520451
- 6 李幼斌20年后重现《亮剑》名场面 7428700
- 7 连霍高速发生交通事故 造成9死7伤 7327682
- 8 今日大雪 要做这些事 7237689
- 9 众擎T800人形机器人一脚踹倒自家CEO 7142427
- 10 中疾控流感防治七问七答 7041226








CSDN
