c#语言特性8.0
Posted zlgan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#语言特性8.0相关的知识,希望对你有一定的参考价值。
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.", ("paper", "scissors") => "paper is cut by scissors. Scissors wins.", ("scissors", "rock") => "scissors is broken by rock. Rock wins.", ("scissors", "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的主要内容,如果未能解决你的问题,请参考以下文章