c#语言特性8.0
2020-12-13 16:28
标签:rgb eric rds 模式 ase mes task reading set c#语言特性8.0 标签:rgb eric rds 模式 ase mes task reading set 原文地址:https://www.cnblogs.com/zlgan/p/11619997.htmlusing 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;
Spanint> numbers = stackalloc int[length];
for (var i = 0; i )
{
numbers[i] = i;
}
//指针类型
unsafe
{
int* p = stackalloc int[10];
for(int i=0;i10;i++)
{
p[i] = i;
}
for(int i=0;i10;i++)
{
Console.WriteLine(*p++);
}
}
}
public enum Rainbow
{
Red,
Orange,
Yellow,
Green,
Blue,
Indigo,
Violet
}
}
}
上一篇:?Delphi开发WebService给ASP.NET调用
下一篇:maven报错 java.lang.RuntimeException: com.google.inject.CreationException: Unable to create injector,