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; t; } public location location { get; t; }}class location{ public string country { get; t; }}
如果location为null,则不会匹配模式并返回fal。
c# 10 引入了一种新的命名空间声明方式 – 文件范围的命名空间,减少一个大括号,代码结构更简洁。
namespace filescopednamespace;class program{ static void main(string[] args) { console.writeline("hello world!"); }}
一次引用,全局通用
global using system;global using system.collections.generic;global using system.linq;global using system.threading.tasks;list<int> list = 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
product product = new() { name = "bread" };console.writeline(product.tostring());// output: breadpublic record product{ public string name { get; init; } public aled override string tostring() { return name; }}
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);
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; t; }}
c# 9 支持本地函数的 attributes, c# 10 添加了 lambda 表达式的 attributes 支持。
action a = [myattribute] () => { }; action<int> b =[return: myattribute] (x) => { }; action<int> c =[myattribute] ([myattribute] x) => { }; class myattribute : attribute{ }
test<int>();var l1 = string () => string.empty;var l2 = int () => 0;var l3 = static void () => { };void test<t>(){ var l4 = t () => default;}
从 c# 7 开始,您只能将asyncmethodbuilder 特性应用于类型, 在 c# 10 中,您还可以将该特性应用于单个方法。
using system.runtime.compilerrvices;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 vegetablepr2019国庆节阅兵观后感oduct tomato = potato with { name = "tomato" };console.writeline($"{tomato.name} {tomato.category}");// output: tomato vegetablestruct product{ public string name { get; t; } public string category { get; t; }}
匿名类型中的表达式触发器ppt爱国让你想起什么
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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 08:57:18,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/9442831efe45de4f0f0dd79f04cde081.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C#10的13个特性.doc
本文 PDF 下载地址:C#10的13个特性.pdf
留言与评论(共有 0 条评论) |