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.","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
        }


    }
}

相关文章

项目中经常遇到CSV文件的读写需求,其中的难点主要是CSV文件...
简介 本文的初衷是希望帮助那些有其它平台视觉算法开发经验的...
这篇文章主要简单记录一下C#项目的dll文件管理方法,以便后期...
在C#中的使用JSON序列化及反序列化时,推荐使用Json.NET——...
事件总线是对发布-订阅模式的一种实现,是一种集中式事件处理...
通用翻译API的HTTPS 地址为https://fanyi-api.baidu.com/api...