gpt4 book ai didi

c# - 在这种情况下,赋值运算符如何工作?

转载 作者:行者123 更新时间:2023-11-30 13:31:37 24 4
gpt4 key购买 nike

这有助于说明我是 c# 的新手。

但是考虑下面的类:

class AllItems
{
private readonly Database database;

public AllItems(Database database)
{
this.database = database;
}
}

我有以下问题:

  1. 在上面的示例中,我们将传递给 AllItems 类的值分配给 private readonly Database database,对吗?

    <
  2. 如果我将 this.database = database 调换成 database = this.database 会不会很重要?

    <
  3. 我们在说 this.database 的地方,this 是指 AllItems(Database database) 中的数据库还是它引用 private readonly Database 数据库 ?

最佳答案

In the above example we are assigning private read-only Database database the value which gets passed into the AllItems class, is that correct?

Would it matter if I swapped this.database = database around, so it looked like database = this.database instead?

是的,会的。您最终要做的是将传入的数据库 parameter 分配为数据库 field 的值(这将是 null默认)。

Where we are saying this.database, does this refer to the database in AllItems(Database database) or does it refer to private read-only Database database?

this是当前类实例的简写,因此它指的是数据库的私有(private)只读成员。

关于c# - 在这种情况下,赋值运算符如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20174190/

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