服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#10的13个特性

C#10的13个特性

2022-12-15 11:29Oleg Kyrylchuk C#

本文详细讲解了C#10的13个特性,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

C#10的13个特性

常量的内插字符串

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

?
1
2
3
4
5
const string name = "Oleg";
const string greeting = $"Hello, {name}.";
 
Console.WriteLine(greeting);
// Output: Hello, Oleg.

扩展属性模式

从 C# 10 开始,您可以在适当的模式中引用嵌套的属性或字段, 属性模式变得更具可读性并且需要更少的大括号。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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,则不会匹配模式并返回false。

文件范围的命名空间

C# 10 引入了一种新的命名空间声明方式 - 文件范围的命名空间,减少一个大括号,代码结构更简洁。

?
1
2
3
4
5
6
7
8
9
namespace FileScopedNamespace;
 
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");
    }
}

全局 Using

一次引用,全局通用

?
1
2
3
4
5
6
7
8
9
10
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);

同一个解构中的赋值和声明

C# 10 可以在同一个解构中进行赋值和声明。

?
1
2
3
4
5
6
7
8
var rgb = (255, 100, 30);
 
// Initialization & assignment
int r;
(r, int g, int b) = rgb;
 
Console.WriteLine($"RGB: {r}, {g}, {b}");
// Output: RGB: 255, 100, 30

Record 类型重写 ToString() 时支持密封

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 字段支持初始化

?
1
2
3
4
5
6
7
8
9
10
11
12
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 表达式的 Attributes 支持

C# 9 支持本地函数的 Attributes, C# 10 添加了 Lambda 表达式的 Attributes 支持。

?
1
2
3
4
5
6
7
Action a = [MyAttribute] () => { };               
Action<int> b =[return: MyAttribute] (x) => { }; 
Action<int> c =[MyAttribute] ([MyAttribute] x) => { };      
 
 
class MyAttribute : Attribute
{ }

Lambda 中的显式返回类型

?
1
2
3
4
5
6
7
8
9
10
Test<int>();
 
var l1 = string () => string.Empty;
var l2 = int () => 0;
var l3 = static void () => { };
 
void Test<T>()
{
    var l4 = T () => default;
}

应用于方法的 AsyncMethodBuilder 特性

从 C# 7 开始,您只能将AsyncMethodBuilder 特性应用于类型, 在 C# 10 中,您还可以将该特性应用于单个方法。

?
1
2
3
4
5
6
7
8
9
using System.Runtime.CompilerServices;
 
class Example
{
    [AsyncMethodBuilder(typeof(AsyncVoidMethodBuilder))]
    public void ExampleMethod()
    {
    }
}

结构体中的表达式

C# 10 支持 将 with 表达式和 struct 一起使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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; }
}

匿名类型中的表达式

C# 10 支持 将 with 表达式和匿名类型一起使用

?
1
2
3
4
5
6
7
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
 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://mp.weixin.qq.com/s/lrHV5rXwY1Z4azPJXW-pGQ

延伸 · 阅读

精彩推荐
  • C#解析C#设计模式编程中备忘录模式的运用

    解析C#设计模式编程中备忘录模式的运用

    这篇文章主要介绍了C#设计模式编程中备忘录模式的运用,备忘录模式用来保存与对象有关的数据用以在将来对对象进行复原,需要的朋友可以参考下...

    Learning hard7662021-11-14
  • C#C#实现简单加减乘除计算器

    C#实现简单加减乘除计算器

    这篇文章主要为大家详细介绍了C#实现简单加减乘除计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    ameyume10482022-02-20
  • C#UGUI实现4位验证码输入

    UGUI实现4位验证码输入

    这篇文章主要为大家详细介绍了UGUI实现4位验证码输入,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    DoyoFish11562022-08-11
  • C#PropertyGrid自定义控件使用详解

    PropertyGrid自定义控件使用详解

    这篇文章主要为大家详细介绍了PropertyGrid自定义控件的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    WCode4252022-01-07
  • C#深入理解C#之继承

    深入理解C#之继承

    这篇文章主要介绍了c# 继承的相关资料,文中讲解的非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下,希望能给你带来帮助...

    佳园9232022-11-25
  • C#WPF的ListView控件自定义布局用法实例

    WPF的ListView控件自定义布局用法实例

    这篇文章主要介绍了WPF的ListView控件自定义布局的方法,结合实例形式分析了WPF中ListView控件的布局方法,需要的朋友可以参考下...

    kagula10042021-11-30
  • C#浅谈C#中HttpWebRequest与HttpWebResponse的使用方法

    浅谈C#中HttpWebRequest与HttpWebResponse的使用方法

    本篇文章主要介绍了浅谈C#中HttpWebRequest与HttpWebResponse的使用方法,具有一定的参考价值,有兴趣的可以了解一下。...

    方倍工作室7712021-12-16
  • C#详解C#压缩、解压文件夹/文件(带密码)

    详解C#压缩、解压文件夹/文件(带密码)

    这篇文章主要给大家介绍了关于C#压缩、解压文件夹/文件(带密码)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考...

    傅小灰9862022-10-09