概述:主要构造函数是 C# 9 中已经存在的功能,但它仅限于类型,现在在 C# 12 中,此功能扩展到任何 和 。在本文中,我将介绍如何在类中使用主构造函数。recordclassstruct主构造函数是为类或结构创建构造函数的一种更简单的方法,它消除了对仅将参数值分配给这些字段的私有字段和构造函数体的显式声明的需要。使用 Primary Constructor,可以向类声明添加参数,并在类主体中使用这些值。在演示如何使用主构造函数之前,让我们先看一下使用构造函数的默认方式。这就是我们使用初始化某些属性的构造函数创建 Book 类的方式:在第 3 行到第 7 行,有类属性。在第 9 行到第 18 行
主要构造函数是 C# 9 中已经存在的功能,但它仅限于类型,现在在 C# 12 中,此功能扩展到任何 和 。在本文中,我将介绍如何在类中使用主构造函数。recordclassstruct
主构造函数是为类或结构创建构造函数的一种更简单的方法,它消除了对仅将参数值分配给这些字段的私有字段和构造函数体的显式声明的需要。使用 Primary Constructor,可以向类声明添加参数,并在类主体中使用这些值。
在演示如何使用主构造函数之前,让我们先看一下使用构造函数的默认方式。这就是我们使用初始化某些属性的构造函数创建 Book 类的方式:
public class BookDefault
{
public int Id { get; }
public string Title { get; }
public int Pages { get; set; }
private readonly List<decimal> ratings = new List<decimal>();
public decimal? AverageRating => ratings.Any() ? ratings.Average() : 0m;
public BookDefault(int id, string title, IEnumerable<decimal>? rating = null)
{
Id = id;
Title = title;
if (rating?.Any() == true)
{
ratings.AddRange(rating);
}
}
}
- 在第 3 行到第 7 行,有类属性。
- 在第 9 行到第 18 行,有构造函数。
出于演示目的,我创建了一个使用 .NET 8 的控制台应用程序,并在 .csproj 文件中配置了以下值:LangVersionpreview
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
</PropertyGroup>
</Project>
现在,在 C# 12 中,使用 Primary Constructor,我们可以获得相同的结果,但代码行数更少:
public class Book(int id, string title, IEnumerable<decimal> ratings)
{
public int Id => id;
public string Title => title.Trim();
public int Pages { get; set; }
public decimal AverageRating => ratings.Any() ? ratings.Average() : 0m;
}
- 在第 1 行,有一个使用 Primary Constructor 的类声明,它有三个属性:id、title 和 ratings。
- 在第 3 行和第 4 行中,有两个只读属性,其值将通过 Primary Constructor 接收。
- 在第 6 行上有一个属性,可以在该属性上读取和写入一些值。
- 在第 8 行,有一个属性可以检索该书的平均评分。
我们也可以有多个构造函数,为此,始终需要使用初始值设定项(它将调用主构造函数)来调用同一类或结构上的另一个构造函数;这可确保始终调用主构造函数,并且存在创建类所需的所有数据。例如:this(…)
public class Book(int id, string title, IEnumerable<decimal> ratings)
{
public Book(int id, string title) : this(id, title, Enumerable.Empty<decimal>()) { }
public Book() : this(99, "Demo book") { }
// The properties...
}
- 在第 1 行,有使用 Primary Constructor 的类声明。
- 在第 3 行,有一个构造函数,它接收 和 。idtitle
- 在第 5 行有一个没有参数的构造函数,它使用默认值(id 99 和标题“Demo book”)初始化对象。
现在让我们初始化这些类的一些实例:
var book1 = new BookDefault(
1,
"The Lord of The Rings the Fellowship of the Ring",
new List<decimal>() { 5, 4, 4, 5 });
var book2 = new Book(
2,
"The Lord of The Rings the Two Towers",
new List<decimal>() { 4, 3, 2, 4 });
book2.Pages = 352;
var book3 = new Book(
3,
"The Lord of the Rings the Return of the King");
var book4 = new Book();
Console.WriteLine(#34;{nameof(book1)}: {JsonSerializer.Serialize(book1)}");
Console.WriteLine(#34;{nameof(book2)}: {JsonSerializer.Serialize(book2)}");
Console.WriteLine(#34;{nameof(book3)}: {JsonSerializer.Serialize(book3)}");
Console.WriteLine(#34;{nameof(book4)}: {JsonSerializer.Serialize(book4)}");
- 对于 ,我们使用的是类,它不使用 Primary 构造函数(它使用默认的构造函数方式)。对于其他实例,我们将使用具有 Primary 构造函数(类)的类。book1BookDefaultBook
- 对于 ,我们用 和 初始化类,然后我们设置 的编号。请注意,无法设置 Id、Title 或 Ratting 等属性的值,因为您将收到以下编译器错误: .book2idtitleratingPagesProperty or indexer ‘property’ cannot be assigned to — it is read only
- 对于 ,我们创建一个仅包含 和 的类的实例,而不定义 ,因此它将使用第二个构造函数,并且 rating 的默认值为 0。book3idtitlerating
- 对于 ,我们创建一个类的实例,而不向它传递任何值,因此它将使用第三个构造函数,该构造函数没有属性,并且默认值将被添加到其中。book4
这将是输出:
book1: {"Id":1,"Title":"The Lord of The Rings the Fellowship of the Ring","Pages":0,"AverageRating":4.5}
book2: {"Id":2,"Title":"The Lord of The Rings the Two Towers","Pages":352,"AverageRating":3.25}
book3: {"Id":3,"Title":"The Lord of the Rings the Return of the King","Pages":0,"AverageRating":0}
book4: {"Id":99,"Title":"Demo book","Pages":0,"AverageRating":0}
依赖注入
您还可以使用主构造函数进行依赖项注入,为此,您需要遵循与上一示例中相同的方法。在演示如何做到这一点之前,让我们先看一下我们通常是如何进行依赖注入的:
public class BookService
{
private readonly IBookRepository _bookRepository;
public BookService(IBookRepository bookRepository)
{
_bookRepository = bookRepository;
}
public async Task<IEnumerable<Book>> GetAll()
{
return await _bookRepository.GetAll();
}
}
- 在第 1 行,有类声明。
- 在第 3 行,存储库类有一个私有属性。
- 在第 5 行,有一个服务类的构造函数,它通过 DI 接收 IBookRepository。
- 在第 7 行,使用提供的 bookRepository 参数初始化_bookRepository。
使用 Primary Constructor,我们可以将 DI 与类声明一起执行,使代码更简洁并减少行数,例如:
public class BookService(IBookRepository bookRepository)
{
public async Task<IEnumerable<Book>> GetAll()
{
return await bookRepository.GetAll();
}
}
- 在第 1 行,有一个带有 Primary Constructor 的类声明,它允许您传递 via 依赖注入,从而避免了样板代码。bookRepository
结论
类和结构的主构造函数是 C# 12 的一项新功能,它允许您以更简单的方式拥有构造函数。如上所述,还可以将 Primary 构造函数用于 DI,从而减少代码的行数。
源代码获取:私我