C# 10的特性有哪些
发表于:2024-09-23 作者:千家信息网编辑
千家信息网最后更新 2024年09月23日,小编给大家分享一下C# 10的特性有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!常量的内插字符串C# 10 允许使用
千家信息网最后更新 2024年09月23日C# 10的特性有哪些
小编给大家分享一下C# 10的特性有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
常量的内插字符串
C# 10 允许使用在常量字符串初始化中使用插值, 如下
const string name = "Oleg";const string greeting = $"Hello, {name}.";Console.WriteLine(greeting);// Output: Hello, Oleg.
扩展属性模式
从 C# 10 开始,您可以在适当的模式中引用嵌套的属性或字段, 属性模式变得更具可读性并且需要更少的大括号。
Person person = new(){ Name = "Oleg", Location = new() { Country = "PL" }};if (person is { Name: "Oleg", Location.Country: "PL" }){ Console.WriteLine("It's me!");}class Person{ public string Name { get; set; } public Location Location { get; set; }}class Location{ public string Country { get; set; }}
如果Location为null,则不会匹配模式并返回false。
文件范围的命名空间
C# 10 引入了一种新的命名空间声明方式 - 文件范围的命名空间,减少一个大括号,代码结构更简洁。
namespace FileScopedNamespace;class Program{ static void Main(string[] args) { Console.WriteLine("Hello World!"); }}
全局 Using
一次引用,全局通用
global using System;global using System.Collections.Generic;global using System.Linq;global using System.Threading.Tasks;Listlist = new() { 1, 2, 3, 4 };int sum = list.Sum();Console.WriteLine(sum);await Task.Delay(1000);
同一个解构中的赋值和声明
C# 10 可以在同一个解构中进行赋值和声明。
var rgb = (255, 100, 30);// Initialization & assignmentint r;(r, int g, int b) = rgb;Console.WriteLine($"RGB: {r}, {g}, {b}");// Output: RGB: 255, 100, 30
Record 类型重写 ToString() 时支持密封
Product product = new() { Name = "Bread" };Console.WriteLine(product.ToString());// Output: Breadpublic record Product{ public string Name { get; init; } public sealed override string ToString() { return Name; }}
Record Struct
C# 10 支持 record struct
Person me = new() { FirstName = "Oleg", LastName = "Kyrylchuk" };Console.WriteLine(me);// Output: Person { FirstName = Oleg, LastName = Kyrylchuk }Person otherPerson = me with { FirstName = "John" };Console.WriteLine(otherPerson);// Output: Person { FirstName = John, LastName = Kyrylchuk }Person anotherMe = new() { FirstName = "Oleg", LastName = "Kyrylchuk" };C onsole.WriteLine(me == anotherMe);// Output: Truerecord struct Person{ public string FirstName { get; init; } public string LastName { get; init; }}record struct Product(string Name, decimal Price);
Struct 字段支持初始化
using System;Person person = new() { Name = "Oleg" };Console.WriteLine(person.Id + " " + person.Name);// Output: 0cc6caac-d061-4f46-9301-c7cc2a012e47 Olegstruct Person{ public Guid Id { get; init; } = Guid.NewGuid(); public string Name { get; set; }}
Lambda 表达式的 Attributes 支持
C# 9 支持本地函数的 Attributes, C# 10 添加了 Lambda 表达式的 Attributes 支持。
Action a = [MyAttribute] () => { }; Actionb =[return: MyAttribute] (x) => { }; Action c =[MyAttribute] ([MyAttribute] x) => { }; class MyAttribute : Attribute{ }
Lambda 中的显式返回类型
Test();var l1 = string () => string.Empty;var l2 = int () => 0;var l3 = static void () => { };void Test (){ var l4 = T () => default;}
应用于方法的 AsyncMethodBuilder 特性
从 C# 7 开始,您只能将AsyncMethodBuilder 特性应用于类型, 在 C# 10 中,您还可以将该特性应用于单个方法。
using System.Runtime.CompilerServices;class Example{ [AsyncMethodBuilder(typeof(AsyncVoidMethodBuilder))] public void ExampleMethod() { }}
结构体中的表达式
C# 10 支持 将 with 表达式和 struct 一起使用
Product potato = new() { Name = "Potato", Category = "Vegetable" };Console.WriteLine($"{potato.Name} {potato.Category}");// Output: Potato VegetableProduct tomato = potato with { Name = "Tomato" };Console.WriteLine($"{tomato.Name} {tomato.Category}");// Output: Tomato Vegetablestruct Product{ public string Name { get; set; } public string Category { get; set; }}
匿名类型中的表达式
C# 10 支持 将 with 表达式和匿名类型一起使用
var potato = new { Name = "Potato", Category = "Vegetable" };Console.WriteLine($"{potato.Name} {potato.Category}");// Output: Potato Vegetablevar onion = potato with { Name = "Onion" };Console.WriteLine($"{onion.Name} {onion.Category}");// Output: Onion Vegetable
以上是"C# 10的特性有哪些"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
C#
支持
表达式
特性
类型
模式
属性
空间
篇文章
应用
全局
内容
字段
字符
字符串
常量
括号
文件
方法
结构
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技公司发票
移动网络技术的英文
网络安全实验室工作待遇
无线传感网络技术期末试题
怎么更换连接数据库ip
2021年 网络安全宣传周
广州暴雨网络技术
山东移动通信网络安全
金山区机械软件开发定制价格
河北区媒体网络技术售后服务
白银分析交易软件开发
东台辉之悦网络技术服务中心
中船网络技术有限公司
网络安全公开课活动总结
湖北软件开发服务
服务器出现错误请联系管理员
亿人服务器集群价格
正版软件开发公司骗局
金山毒霸 服务器
net软件开发毕业去向
马超软件开发书籍
公司培训软件开发工程师
陕西省ipfs云服务器云空间
Excel 数据库 随机
内外网网络安全交互
建筑行业网络安全规范有哪些
网络安全法维护了那些权益
饥荒怎么创建服务器
网络安全国家一级重点学科
网络安全威胁包