C# 9 特性三
介绍
接下来我将给大家重点介绍一下.Net 6 之后的一些新的变更,文章都是来自于外国大佬的文章,我这边进行一个翻译,并加上一些自己的理解和解释。
源作者链接:https://blog.okyrylchuk.dev/a-comprehensive-overview-of-c-9-features
正文
扩展“GetEnumerator”支持“foreach”循环
在 C#IEnumerator
public static class Extensions
{
public static IEnumerator<T> GetEnumerator<T>
(this IEnumerator<T> enumerator) => enumerator;
}
class Program
{
static void Main()
{
var list = new List<int> { 1, 2, 3 };
IEnumerator<int> enumerator = list.GetEnumerator();
foreach (int i in enumerator)
{
Console.WriteLine(i);
}
// Output:
// 1
// 2
// 3
}
}
本机大小的整数
C# 9 引入了新的关键字nint并nuint定义了原生大小的整数。在 32 位进程中运行时为 32 位整数。在 64 位进程中运行时为 64 位整数。它们可用于互操作场景和低级库。
// The compiler provides operations and conversions for
// 'nint' and 'nuint' that are appropriate for integer types.
nint a = 10;
nint b = 7;
nuint c = 5;
nuint d = 3;
函数指针
C# 9 使用delegate*语法引入了真正的函数指针。仅在unsafe上下文中有效
unsafe class Example
{
// This method has a managed calling convention.
// This is the same as leaving the 'managed' keyword off.
delegate*<int, void> functionPointer1
// The same as functionPointer1, but with explicit 'managed' keyword
delegate* managed<int, void> functionPointer2
// This method will be invoked using whatever the default unmanaged calling
// convention on the runtime platform is. This is platform and architecture
// dependent and is determined by the CLR at runtime.
public delegate* unmanaged<int, void> functionPointer3;
}
record
C# 9 引入了一种新的数据类型 - record. 它是类的轻量级不可变(如果它有init道具!)版本。它是一种引用类型,但具有基于值的比较。
record Person
{
// No constructor required to initialize properties
public string FirstName { get; init; } // immutable
public string LastName { get; init; } // immutable
}
static void Main()
{
var person = new Person
{
FirstName = "Oleg",
LastName = "Kyrylchuk"
}
// Use with-expression to create new record from existing
// specifying the changes in the values of properties
var bondPerson = person with { LastName = "Bond" }
// Value base comparison
var duplicatedPerson = new Person
{
FirstName = "Oleg",
LastName = "Kyrylchuk"
};
person.Equals(duplicatedPerson); // true
_ = person == duplicatedPerson; // true
}
位置记录
C# 9 中的记录允许构造函数和解构函数。但是,它需要大量代码。您可以使用位置记录省略大部分代码
// Positional record
// Immutable auto properties are created by position
// Constructor and deconstructor (by position) are here
record Person(string FirstName, string LastName);
static void Main()
{
// Construct record
var person = new Person("Oleg", "Kyrylchuk");
Console.WriteLine(person.FirstName);
Console.WriteLine(person.LastName)
// Deconstruct record
var (firstName, lastName) = person;
Console.WriteLine(firstName);
Console.WriteLine(lastName);
}
记录与继承
C# 9 中的记录可以继承记录(不能类)。记录有一个隐藏的虚拟方法,可以克隆整个记录。每个派生记录都会覆盖它以调用复制构造函数并将其与基记录的复制构造函数链接起来。
record Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
record Employee : Person
{
public string Position { get; init; }
static void Main()
{
Person person = new Employee
{
FirstName = "Oleg",
LastName = "Kyrylchuk",
Position = ".NET Developer"
}
// Hidden virtual method copies the whole record (as Employee)
Person newPerson = person with { LastName = "Bond" }
Employee employee = newPerson as Employee;
Console.WriteLine(employee.FirstName); // Oleg
Console.WriteLine(employee.LastName); // Bond
Console.WriteLine(employee.Position); // .NET Developer
}
结语
联系作者:加群:867095512 @MrChuJiu