c#语言特性8.0

c#语言特性8.0,第1张

概述using DocumentFormat.OpenXml.Presentation;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Language{ class
using documentFormat.OpenXml.Presentation;using System;using System.Collections.Generic;using System.linq;using System.Text;using System.Threading.Tasks;namespace Language{    class Class1    {        //Readonly 成员        public struct Point        {            public double X { get; set; }            public double Y { get; set; }            public double distance => Math.Sqrt(X * X + Y * Y);            public Readonly overrIDe string ToString() =>                $"({X},{Y}) is {distance} from the origin";        }        //元组模式        public static string RockPaperScissors(string first,string second)    => (first,second) switch    {        ("rock","paper") => "rock is covered by paper. Paper wins.",("rock","scissors") => "rock breaks scissors. Rock wins.",("paper","rock") => "paper covers rock. Paper wins.","scissors") => "paper is cut by scissors. Scissors wins.",("scissors","rock") => "scissors is broken by rock. Rock wins.","paper") => "scissors cuts paper. Scissors wins.",(_,_) => "tIE"    };        /* 这里有几个语法改进:变量位于 switch 关键字之前。 不同的顺序使得在视觉上可以很轻松地区分 switch 表达式和 switch 语句。将 case 和 : 元素替换为 =>。 它更简洁,更直观。将 default 事例替换为 _ 弃元。正文是表达式,不是语句。 */        public static Rgbcolor Fromrainbow(Rainbow colorBand)        {            return colorBand switch            {                Rainbow.Red => new Rgbcolor(),Rainbow.Orange => new Rgbcolor(),Rainbow.Yellow => new Rgbcolor(),Rainbow.Green => new Rgbcolor(),Rainbow.Blue => new Rgbcolor(),Rainbow.Indigo => new Rgbcolor(),Rainbow.Violet => new Rgbcolor(),_ => throw new ArgumentException(message: "invalID enum value",paramname: nameof(colorBand)),};        }        //静态本地函数        int M()        {            int y = 5;            int x = 7;            return Add(x,y);            static int Add(int left,int right) => left + right;        }        public static voID Run()        {            //索引和范围            var words = new string[]            {                        // index from start    index from end            "The",// 0                   ^9            "quick",// 1                   ^8            "brown",// 2                   ^7            "fox",// 3                   ^6            "jumped",// 4                   ^5            "over",// 5                   ^4            "the",// 6                   ^3            "lazy",// 7                   ^2            "dog"       // 8                   ^1            };              // 9 (or words.Length) ^0            //words[1..4]  "The","quick","brown","fox",//words[4..]    "jumped"---->"dog"            //stackalloc 运算符            int length = 3;            Span<int> numbers = stackalloc int[length];            for (var i = 0; i < length; i++)            {                numbers[i] = i;            }            //指针类型            unsafe            {                int* p = stackalloc int[10];                for(int i=0;i<10;i++)                {                    p[i] = i;                }                for(int i=0;i<10;i++)                {                    Console.Writeline(*p++);                }            }        }        public enum Rainbow        {            Red,Orange,Yellow,Green,Blue,Indigo,Violet        }    }}
总结

以上是内存溢出为你收集整理的c#语言特性8.0全部内容,希望文章能够帮你解决c#语言特性8.0所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1214214.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存