c # 8.0 language features

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") => "Broken by IS Rock Rock Scissors WINS.. " , 
        ( " Scissors " , " Paper " ) => " Scissors Scissors Cuts Paper WINS.. " , 
        (_, _) => " TIE " 
    }; 


        / * 
 Here are a few improved syntax: 
variable switch located before the keyword in a different order so that you can easily visually distinguish expressions switch and switch statements. 
the case and: replacing an element => it is more concise and more intuitive. 
the default case replaced _ to abandon yuan 
body is an expression, not a statement. 
 * / 
        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)), 


            }; 
        } 

        // static local function 
        int M () 
        { 
            int Y = . 5 ;
             int X = . 7 ;
             return the Add (X, Y); 

            static  int the Add ( int left, int right) => left + right; 
        } 

        public  static  void the Run () 
        { 
            // index and scope 
            var words = new 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];
            0i =were(for; 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
        }


    }
}

 

Guess you like

Origin www.cnblogs.com/zlgan/p/11619997.html