欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#10的13個(gè)特性

 更新時(shí)間:2021年12月31日 09:37:44   作者:Oleg?Kyrylchuk  
本文詳細(xì)講解了C#10的13個(gè)特性,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

常量的內(nèi)插字符串

C# 10 允許使用在常量字符串初始化中使用插值, 如下

const string name = "Oleg";
const string greeting = $"Hello, {name}.";

Console.WriteLine(greeting);
// Output: Hello, Oleg.

擴(kuò)展屬性模式

從 C# 10 開始,您可以在適當(dāng)?shù)哪J街幸们短椎膶傩曰蜃侄危?屬性模式變得更具可讀性并且需要更少的大括號(hào)。

Person person = new()
{
    Name = "Oleg",
    Location = new() { Country = "PL" }
};

if (person is { Name: "Oleg", Location.Country: "PL" })
{
    Console.WriteLine("It's me!");
}

class Person
{
    public string Name { get; set; }
    public Location Location { get; set; }
}

class Location
{
    public string Country { get; set; }
}

如果Location為null,則不會(huì)匹配模式并返回false。

文件范圍的命名空間

C# 10 引入了一種新的命名空間聲明方式 - 文件范圍的命名空間,減少一個(gè)大括號(hào),代碼結(jié)構(gòu)更簡潔。

namespace FileScopedNamespace;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
    }
}

全局 Using

一次引用,全局通用

global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading.Tasks;

List<int> list = new() { 1, 2, 3, 4 };
int sum = list.Sum();
Console.WriteLine(sum);

await Task.Delay(1000);

同一個(gè)解構(gòu)中的賦值和聲明

C# 10 可以在同一個(gè)解構(gòu)中進(jìn)行賦值和聲明。

var rgb = (255, 100, 30);

// Initialization & assignment
int r;
(r, int g, int b) = rgb;

Console.WriteLine($"RGB: {r}, {g}, ");
// Output: RGB: 255, 100, 30

Record 類型重寫 ToString() 時(shí)支持密封

Product product = new() { Name = "Bread" };
Console.WriteLine(product.ToString());
// Output: Bread

public record Product
{
    public string Name { get; init; }

    public sealed override string ToString()
    {
        return Name;
    }
}

Record Struct

C# 10 支持 record struct

Person me = new() { FirstName = "Oleg", LastName = "Kyrylchuk" };

Console.WriteLine(me);
// Output: Person { FirstName = Oleg, LastName = Kyrylchuk }

Person otherPerson = me with { FirstName = "John" };
Console.WriteLine(otherPerson);
// Output: Person { FirstName = John, LastName = Kyrylchuk }

Person anotherMe = new() { FirstName = "Oleg", LastName = "Kyrylchuk" };
C onsole.WriteLine(me == anotherMe);
// Output: True

record struct Person
{
    public string FirstName { get; init; }
    public string LastName { get; init; }
}

record struct Product(string Name, decimal Price);

Struct 字段支持初始化

using System;

Person person = new() { Name = "Oleg" };

Console.WriteLine(person.Id + " " + person.Name);
// Output: 0cc6caac-d061-4f46-9301-c7cc2a012e47 Oleg

struct Person
{
    public Guid Id { get; init; } = Guid.NewGuid();
    public string Name { get; set; }
}

Lambda 表達(dá)式的 Attributes 支持

C# 9 支持本地函數(shù)的 Attributes, C# 10 添加了 Lambda 表達(dá)式的 Attributes 支持。

Action a = [MyAttribute] () => { };                
Action<int> b =[return: MyAttribute] (x) => { };  
Action<int> c =[MyAttribute] ([MyAttribute] x) => { };       


class MyAttribute : Attribute
{ }

Lambda 中的顯式返回類型

Test<int>();

var l1 = string () => string.Empty;
var l2 = int () => 0;
var l3 = static void () => { };

void Test<T>()
{
    var l4 = T () => default;
}

應(yīng)用于方法的 AsyncMethodBuilder 特性

從 C# 7 開始,您只能將AsyncMethodBuilder 特性應(yīng)用于類型, 在 C# 10 中,您還可以將該特性應(yīng)用于單個(gè)方法。

using System.Runtime.CompilerServices;

class Example
{
    [AsyncMethodBuilder(typeof(AsyncVoidMethodBuilder))]
    public void ExampleMethod()
    {
    }
}

結(jié)構(gòu)體中的表達(dá)式

C# 10 支持 將 with 表達(dá)式和 struct 一起使用

Product potato = new() { Name = "Potato", Category = "Vegetable" };
Console.WriteLine($"{potato.Name} {potato.Category}");
// Output: Potato Vegetable

Product tomato = potato with { Name = "Tomato" };
Console.WriteLine($"{tomato.Name} {tomato.Category}");
// Output: Tomato Vegetable

struct Product
{
    public string Name { get; set; }
    public string Category { get; set; }
}

匿名類型中的表達(dá)式

C# 10 支持 將 with 表達(dá)式和匿名類型一起使用

var potato = new { Name = "Potato", Category = "Vegetable" };
Console.WriteLine($"{potato.Name} {potato.Category}");
// Output: Potato Vegetable

var onion = potato with { Name = "Onion" };
Console.WriteLine($"{onion.Name} {onion.Category}");
// Output: Onion Vegetable

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#自適應(yīng)合并文件的方法

    C#自適應(yīng)合并文件的方法

    這篇文章主要介紹了C#自適應(yīng)合并文件的方法,涉及C#基于FileStream類實(shí)現(xiàn)文件讀寫操作的相關(guān)技巧,非常簡單實(shí)用,需要的朋友可以參考下
    2015-07-07
  • C# menuStrip控件實(shí)現(xiàn)鼠標(biāo)滑過自動(dòng)彈出功能

    C# menuStrip控件實(shí)現(xiàn)鼠標(biāo)滑過自動(dòng)彈出功能

    MenuStrip 控件是 Visual Studio 和 .NET Framework 中的功能。使用該控件,可以輕松創(chuàng)建 Microsoft Office 中那樣的菜單。本文給大家分享menuStrip鼠標(biāo)滑過自動(dòng)彈出效果
    2021-07-07
  • C#中使用Socket獲取網(wǎng)頁源代碼的代碼

    C#中使用Socket獲取網(wǎng)頁源代碼的代碼

    C#使用Socket獲取網(wǎng)頁源代碼的代碼,需要的朋友可以參考下。
    2010-12-12
  • c# 泛型類型參數(shù)與約束的深入分析

    c# 泛型類型參數(shù)與約束的深入分析

    本篇文章是對(duì)c#中泛型類型參數(shù)與約束進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C# ManagementObjectSearcher操作window案例詳解

    C# ManagementObjectSearcher操作window案例詳解

    這篇文章主要介紹了C# ManagementObjectSearcher操作window案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • c#操作Redis的5種基本類型匯總

    c#操作Redis的5種基本類型匯總

    這篇文章主要給大家介紹了關(guān)于c#操作Redis的5種基本類型,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Unity Shader實(shí)現(xiàn)玻璃材質(zhì)效果

    Unity Shader實(shí)現(xiàn)玻璃材質(zhì)效果

    這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)玻璃材質(zhì)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 最新評(píng)論