主要构造函数是 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);
}
}
}
出于演示目的,我创建了一个使用 .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;
}
我们也可以有多个构造函数,为此,始终需要使用初始值设定项(它将调用主构造函数)来调用同一类或结构上的另一个构造函数;这可确保始终调用主构造函数,并且存在创建类所需的所有数据。例如: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...
}
现在让我们初始化这些类的一些实例:
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($"{nameof(book1)}: {JsonSerializer.Serialize(book1)}");
Console.WriteLine($"{nameof(book2)}: {JsonSerializer.Serialize(book2)}");
Console.WriteLine($"{nameof(book3)}: {JsonSerializer.Serialize(book3)}");
Console.WriteLine($"{nameof(book4)}: {JsonSerializer.Serialize(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();
}
}
使用 Primary Constructor,我们可以将 DI 与类声明一起执行,使代码更简洁并减少行数,例如:
public class BookService(IBookRepository bookRepository)
{
public async Task<IEnumerable<Book>> GetAll()
{
return await bookRepository.GetAll();
}
}
结论
类和结构的主构造函数是 C# 12 的一项新功能,它允许您以更简单的方式拥有构造函数。如上所述,还可以将 Primary 构造函数用于 DI,从而减少代码的行数。
源代码获取:公众号回复消息【code:42514
】