gpt4 book ai didi

c# - 使用这个的简单构造函数

转载 作者:太空宇宙 更新时间:2023-11-03 18:36:49 25 4
gpt4 key购买 nike

我正在检查一些代码,我发现了下面这段代码,我只是想确定我对第二个构造函数的理解。所以,请确认我对 :this()

的理解是否正确

当使用第二个构造函数创建 User 时,它将始终继承分配的 Roles 属性,因为 Roles 属性未在第二个构造函数内的任何位置分配,我假设它留待稍后在代码中的某个地方使用。

protected User()
{
Roles = new HashedSet<Role>();
}

public User(string username, string email, string password, string hashAlgorithm)
: this()
{
UserName = username;
Email = email;
SetPassword(password, hashAlgorithm);
IsApproved = true;
}

最佳答案

“this()”只是调用第一个构造函数。请参阅以下 MSDN 主题中的最后两个代码片段:

http://msdn.microsoft.com/en-us/library/ms173115.aspx

关于c# - 使用这个的简单构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14094610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com