加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#语言特性8.0

发布时间:2020-12-16 01:19:31 所属栏目:百科 来源:网络整理
导读: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
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
        }


    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读